Code Monkey home page Code Monkey logo

secondarytabview's Introduction

SecondaryTabView

A secondary tab navigation view that appears on the top of the screen. Smooth animations.

SecondaryTabView.mov

Motivation

The motivation comes from the deficiency of our existing navigation options. A tab bar provides primary navigation, and if a secondary tab is needed, it requires us to make custom views with stack navigations. This secondary tab navigation is a compact and customizable solution that leverages the power of SwiftUI to make ends meet.

Usage

The screen recording is from the preview code in SecondaryTabView.swift. Refer to that for the full details.

  1. Define an item type representing each tab's view model. A simple enum is usually sufficient.
private enum Item: Int, SecondaryTabViewItem {
    case first = 0
    case second
    case third
    case fourth

    var text: String {
        switch self {
        case .first: return "First"
        case .second: return "Second"
        case .third: return "Third"
        case .fourth: return "Fourth name too long"
        }
    }
}
  1. In your container view, declare a selection state.
@State private var selection: Item = .first
  1. Now you can use the secondary tab view as below.

a. You want to bind your selection state. b. Declare each tab's content view identify each with .tabItem(for: <Instance of Item>). In the example below the content is a simple Text view, but it can be anything you want. c. Provide a label for each tab. You may customize the appearance based on the selection state. d. There are customizations for the selection underline and view modifier.

SecondaryTabView(
    selection: $selection,
    content: {
        Text(verbatim: "First")
            .tabItem(for: Item.first)

        Text(verbatim: "Second")
            .tabItem(for: Item.second)

        Text(verbatim: "Third")
            .tabItem(for: Item.third)

        Text(verbatim: "Fourth name too long")
            .tabItem(for: Item.fourth)
    },
    label: { item, isSelected in
        Text(
            item.text
        )
        .foregroundColor(isSelected ? Color.orange : Color.primary)
    },
    underline: {
        Color.orange
        .frame(height: 2)
    },
    barViewModifier: ShadowModifier()
)

secondarytabview's People

Contributors

djben avatar

Stargazers

 avatar Mou avatar

Watchers

 avatar

Forkers

azuredark

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.