From 1b39b3e7040d53640bc5da9fa211d58f98caeba6 Mon Sep 17 00:00:00 2001 From: Nicholas Bellucci Date: Mon, 30 Nov 2020 17:51:25 -0500 Subject: [PATCH] Removed binding requirement for selected tab --- README.md | 6 ++++-- .../Helpers/TabBarController.swift | 3 +-- Sources/StatefulTabView/StatefulTabView.swift | 19 ++++++------------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f27968d..eebb1ad 100644 --- a/README.md +++ b/README.md @@ -73,10 +73,12 @@ StatefulTabView { ### Selected Index -The selected index of the StatefulTabView can be set within the initializer. The passed value is a binding. +The selected index of the StatefulTabView can be set within the initializer. ```Swift -StatefulTabView(selectedIndex: $selectedIndex) { +@State var selectedIndex: Int = 2 + +StatefulTabView(selectedIndex: selectedIndex) { ... } ``` diff --git a/Sources/StatefulTabView/Helpers/TabBarController.swift b/Sources/StatefulTabView/Helpers/TabBarController.swift index f374fc0..5fe78ff 100644 --- a/Sources/StatefulTabView/Helpers/TabBarController.swift +++ b/Sources/StatefulTabView/Helpers/TabBarController.swift @@ -21,8 +21,7 @@ struct TabBarController: UIViewControllerRepresentable { var unselectedItemTintColor: UIColor? var backgroundColor: UIColor? var tabBarConfiguration: TabBarBackgroundConfiguration? - - @Binding var selectedIndex: Int + var selectedIndex: Int func makeUIViewController(context: Context) -> UITabBarController { let tabBarController = UITabBarController() diff --git a/Sources/StatefulTabView/StatefulTabView.swift b/Sources/StatefulTabView/StatefulTabView.swift index 1d7f04c..1f4e67b 100644 --- a/Sources/StatefulTabView/StatefulTabView.swift +++ b/Sources/StatefulTabView/StatefulTabView.swift @@ -10,26 +10,19 @@ import SwiftUI 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: Binding? = nil, @TabBuilder _ content: () -> [Tab]) { - if let selectedIndex = selectedIndex { - _bindableIndex = selectedIndex - useBindableIndex = true - } else { - _bindableIndex = .constant(0) - useBindableIndex = false - } - + public init(selectedIndex: Int = 0, @TabBuilder _ content: () -> [Tab]) { + self.selectedIndex = selectedIndex configureViewControllers(with: content()) } @@ -40,7 +33,7 @@ public struct StatefulTabView: View { unselectedItemTintColor: unselectedItemTintColor, backgroundColor: backgroundColor, tabBarConfiguration: tabBarConfiguration, - selectedIndex: useBindableIndex ? $bindableIndex : $stateIndex) + selectedIndex: selectedIndex) .edgesIgnoringSafeArea(.all) } }