Skip to content

Commit

Permalink
Merge pull request #8 from NicholasBellucci/feature/unselected-tint-c…
Browse files Browse the repository at this point in the history
…olor

Feature/unselected tint color
  • Loading branch information
NicholasBellucci authored Nov 30, 2020
2 parents c7ce860 + 1b39b3e commit 952e535
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 52 deletions.
5 changes: 3 additions & 2 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import SwiftUI

struct ContentView: View {
@State var badgeValue1: String? = "1"
@State var badgeValue: String? = "1"

var body: some View {
StatefulTabView {
Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue1) {
Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue) {
NavigationView {
List {
Section {
Expand Down Expand Up @@ -73,6 +73,7 @@ struct ContentView: View {
}
}
.barTintColor(.red)
.unselectedItemTintColor(.green)
.barBackgroundColor(.yellow)
.barAppearanceConfiguration(.transparent)
}
Expand Down
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ In Xcode 11 or greater, navigate to `File > Swift Packages > Add Package Depende
* [Appearance Modifications](#appearance-modifications)
* [Selected Index](#selected-index)
* [Badge Value](#badge-value)
* [Single Tab](#single-tab)
* [Scroll to Top with Large Titles](#scroll-to-top-with-large-titles)
* [License](#license)

Expand Down Expand Up @@ -67,39 +66,32 @@ 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.
The selected index of the StatefulTabView can be set within the initializer.

```Swift
StatefulTabView(selectedIndex: $selectedIndex) {
@State var selectedIndex: Int = 2

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

### Badge Value

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

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

### Single Tab
@State var badgeValue: String = "1"

Due to the limitations of the current `@_functionBuilder` implementation in Swift, to build a StatefulTabView with one tab `BuilderType.individual`, should be passed within the initializer.

```Swift
StatefulTabView(.individual) {
Tab(title: "Tab 1", systemImageName: "circle.fill") {
...
}
Tab(title: "Tab 1", systemImageName: "circle.fill", badgeValue: badgeValue) {
...
}
```

Expand Down
13 changes: 11 additions & 2 deletions Sources/StatefulTabView/Helpers/TabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ struct TabBarController: UIViewControllerRepresentable {
var tabBarItems: [Tab]

var barTintColor: UIColor?
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 Expand Up @@ -68,6 +68,15 @@ private extension TabBarController {
if let barTintColor = barTintColor {
tabBar.tintColor = barTintColor
}

if let unselectedItemTintColor = unselectedItemTintColor {
if #available(iOS 13.0, *) {
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: unselectedItemTintColor]
appearance.stackedLayoutAppearance.normal.iconColor = unselectedItemTintColor
} else {
tabBar.unselectedItemTintColor = unselectedItemTintColor
}
}

if let backgroundColor = backgroundColor {
tabBar.backgroundColor = backgroundColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public extension StatefulTabView {
copy.barTintColor = color
return copy
}

func unselectedItemTintColor(_ color: UIColor) -> StatefulTabView {
var copy = self
copy.unselectedItemTintColor = color
return copy
}

func barBackgroundColor(_ color: UIColor) -> StatefulTabView {
var copy = self
Expand Down
43 changes: 12 additions & 31 deletions Sources/StatefulTabView/StatefulTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,33 @@

import SwiftUI

public enum BuilderType {
case individual
}

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, _ type: BuilderType, _ content: () -> Tab) {
if let selectedIndex = selectedIndex {
_bindableIndex = selectedIndex
useBindableIndex = true
} else {
_bindableIndex = .constant(0)
useBindableIndex = false
}

let tabController = UIHostingController(rootView: content().view)
tabController.tabBarItem = content().barItem
viewControllers.append(tabController)
}

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())
}

public var body: some View {
TabBarController(controllers: viewControllers,
tabBarItems: tabBarItems,
barTintColor: barTintColor,
unselectedItemTintColor: unselectedItemTintColor,
backgroundColor: backgroundColor,
tabBarConfiguration: tabBarConfiguration,
selectedIndex: useBindableIndex ? $bindableIndex : $stateIndex)
selectedIndex: selectedIndex)
.edgesIgnoringSafeArea(.all)
}
}
Expand All @@ -77,4 +54,8 @@ public struct TabBuilder {
public static func buildBlock(_ children: Tab...) -> [Tab] {
children
}

public static func buildBlock(_ component: Tab) -> [Tab] {
[component]
}
}

0 comments on commit 952e535

Please sign in to comment.