diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index e136ab9..5f216b5 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -216,7 +216,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1140; - LastUpgradeCheck = 1140; + LastUpgradeCheck = 1220; ORGANIZATIONNAME = "Nicholas Bellucci"; TargetAttributes = { 4C6A94472468D15000D5C6C7 = { @@ -363,6 +363,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -423,6 +424,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; diff --git a/Example/Example/ContentView.swift b/Example/Example/ContentView.swift index f52e531..923252a 100644 --- a/Example/Example/ContentView.swift +++ b/Example/Example/ContentView.swift @@ -9,10 +9,11 @@ import SwiftUI struct ContentView: View { + @State var selectedIndex: Int = 1 @State var badgeValue: String? = "1" - + var body: some View { - StatefulTabView { + StatefulTabView(selectedIndex: $selectedIndex) { Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue) { NavigationView { List { @@ -29,7 +30,7 @@ struct ContentView: View { } Tab(title: "Tab 2", systemImageName: "square.fill") { - NavigationView{ + NavigationView { List { Section { ForEach(0..<20, id: \.self) { index in diff --git a/README.md b/README.md index eebb1ad..807f233 100644 --- a/README.md +++ b/README.md @@ -73,12 +73,12 @@ StatefulTabView { ### Selected Index -The selected index of the StatefulTabView can be set within the initializer. +The selected index of the StatefulTabView can be set within the initializer. The passed value is a binding. ```Swift @State var selectedIndex: Int = 2 -StatefulTabView(selectedIndex: selectedIndex) { +StatefulTabView(selectedIndex: $selectedIndex) { ... } ``` diff --git a/Sources/StatefulTabView/Helpers/TabBarController.swift b/Sources/StatefulTabView/Helpers/TabBarController.swift index 5fe78ff..f374fc0 100644 --- a/Sources/StatefulTabView/Helpers/TabBarController.swift +++ b/Sources/StatefulTabView/Helpers/TabBarController.swift @@ -21,7 +21,8 @@ struct TabBarController: UIViewControllerRepresentable { var unselectedItemTintColor: UIColor? var backgroundColor: UIColor? var tabBarConfiguration: TabBarBackgroundConfiguration? - var selectedIndex: Int + + @Binding var selectedIndex: Int func makeUIViewController(context: Context) -> UITabBarController { let tabBarController = UITabBarController() diff --git a/Sources/StatefulTabView/StatefulTabView.swift b/Sources/StatefulTabView/StatefulTabView.swift index 1f4e67b..5970a5f 100644 --- a/Sources/StatefulTabView/StatefulTabView.swift +++ b/Sources/StatefulTabView/StatefulTabView.swift @@ -11,18 +11,25 @@ public struct StatefulTabView: View { internal var viewControllers: [UIHostingController] = [] internal var tabBarItems: [Tab] = [] - internal var selectedIndex: Int internal var barTintColor: UIColor? = nil internal var unselectedItemTintColor: UIColor? = nil internal var backgroundColor: UIColor? = nil internal var tabBarConfiguration: TabBarBackgroundConfiguration? = nil @State private var stateIndex: Int = 0 + @Binding private var bindableIndex: Int private var useBindableIndex: Bool = false - public init(selectedIndex: Int = 0, @TabBuilder _ content: () -> [Tab]) { - self.selectedIndex = selectedIndex + public init(selectedIndex: Binding? = nil, @TabBuilder _ content: () -> [Tab]) { + if let selectedIndex = selectedIndex { + _bindableIndex = selectedIndex + useBindableIndex = true + } else { + _bindableIndex = .constant(0) + useBindableIndex = false + } + configureViewControllers(with: content()) } @@ -33,7 +40,7 @@ public struct StatefulTabView: View { unselectedItemTintColor: unselectedItemTintColor, backgroundColor: backgroundColor, tabBarConfiguration: tabBarConfiguration, - selectedIndex: selectedIndex) + selectedIndex: useBindableIndex ? $bindableIndex : $stateIndex) .edgesIgnoringSafeArea(.all) } }