Giter Site home page Giter Site logo

nicholasbellucci / statefultabview Goto Github PK

View Code? Open in Web Editor NEW
311.0 4.0 24.0 51 KB

A SwiftUI TabView that retains the state of each tab as well as some other goodies.

License: MIT License

Swift 100.00%
swiftui tabview swift-package-manager xcode

statefultabview's Introduction

StatefulTabView

A SwiftUI UITabBarController implementation that retains state between tab changes. Big thanks to Amzd and everyone who helped to refine this gist as it was a major jumping off point for setting up this project.

Requirements

  • iOS 13.0+
  • Xcode 11.2+
  • Swift 5+

Installation

Swift Package Manager

In Xcode 11 or greater, navigate to File > Swift Packages > Add Package Dependency.... From there just simply add https://github.com/NicholasBellucci/StatefulTabView as the package repository url and use the master branch or the most recent version. Master will always be inline with the newest release.

Table of Contents

Features

  • State driven selected index
  • TabBar appearance configuration
  • TabBar custom tint color
  • TabBar custom background color
  • TabBarItem custom title and image
  • TabBarItem badge value
  • State retention from tab to tab
  • Pop to root functionality when selecting the already selected tab
  • Scroll to top functionality when selecting the already selected tab at the root view

Usage

Setting up StatefulTabView is relatively simple and works similar to the native TabView. The main difference is that the content of the tabs is wrapped in a Tab struct. There is no limitation on how many tabs can be used. Once more than 5 are used, the Apple standard more tab will become available. Feel free to check out the example project for the exact usage.

Basic

StatefulTabView {
    Tab(title: "Tab 1", systemImageName: "circle.fill") {
        NavigationView {
            List {
                Section {
                    ForEach(0..<20, id: \.self) { index in
                        NavigationLink(destination: PushedView(text: "Pushed number \(index)")) {
                            Text("\(index)")
                        }
                    }
                }
            }
            .navigationBarTitle("Navigation View 1")
        }
    }
}

Appearance Modifications

All appearance modifications can be made by using extensions for the StatefulTabView.

StatefulTabView {
    ...
}
.barTintColor(.red)
.unselectedItemTintColor(.green)
.barBackgroundColor(.yellow)
.barAppearanceConfiguration(.transparent)

Selected Index

The selected index of the StatefulTabView can be set within the initializer. The passed value is a binding.

@State var selectedIndex: Int = 2

StatefulTabView(selectedIndex: $selectedIndex) {
    ...
}

Badge Value

The TabBarItem badge value can be set in the initializer of a Tab.

@State var badgeValue: String = "1"

Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue) {
    ...
}

Scroll to Top with Large Titles

Scroll to top is handled when selecting the already selected tab that contains a scrollView in the heirarchy. The only issue is that large titles in navigation bars are not factored in when calling scrollRectToVisible(CGRect(x: 0, y: 0, width: 1, height: 1), animated: true) for obvious reasons. Due to this limitation adding .prefersLargeTitle(true) to a Tab will fix this issue. For root navigation views that do not use a large title no change to a Tab is needed.

Tab(title: "Tab 1", systemImageName: "circle.fill") {
    NavigationView {
        List {
            ...
        }
        .navigationBarTitle("Navigation View 1", displayMode: .large)
    }
}
.prefersLargeTitle(true)

License

StatefulTabView is, and always will be, MIT licensed. See LICENSE for details.

statefultabview's People

Contributors

camdenfullmer avatar gobetti avatar michaelneas avatar nicholasbellucci avatar p-larson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

statefultabview's Issues

[Maybe a bug] call on onAppear()

I'm not sure on which version is a bug (if it even is).
On iOS 13 onAppear() gets called only once and never again. But, in iOS 14 it gets called on every show of the Tab.

Tested on my device iOS 14.5 and multiple versions of the simulator: 13.0 - 14.0 - 14.5
The behaviour is consistent on all.

In my case, it helped solve the TabView issue on iOS 14. So I use this view only in this version, because I do want the onAppear call. So, thanks?

iOS 15

Doesn't work as well with iOS 15 with my experience with it so far, the divider at the top of the tab bar doesn't appear unless there is "content" in the view behind it... some issues with it disappearing in navigation too...

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.