Skip to content

Commit

Permalink
Removed binding requirement for selected tab
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasBellucci committed Nov 30, 2020
1 parent 7480096 commit 1b39b3e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
...
}
```
Expand Down
3 changes: 1 addition & 2 deletions Sources/StatefulTabView/Helpers/TabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 6 additions & 13 deletions Sources/StatefulTabView/StatefulTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,19 @@ import SwiftUI
public struct StatefulTabView: View {
internal var viewControllers: [UIHostingController<AnyView>] = []
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<Int>? = 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())
}

Expand All @@ -40,7 +33,7 @@ public struct StatefulTabView: View {
unselectedItemTintColor: unselectedItemTintColor,
backgroundColor: backgroundColor,
tabBarConfiguration: tabBarConfiguration,
selectedIndex: useBindableIndex ? $bindableIndex : $stateIndex)
selectedIndex: selectedIndex)
.edgesIgnoringSafeArea(.all)
}
}
Expand Down

0 comments on commit 1b39b3e

Please sign in to comment.