diff --git a/Package.swift b/Package.swift index 0b07719..e044896 100644 --- a/Package.swift +++ b/Package.swift @@ -5,6 +5,9 @@ import PackageDescription let package = Package( name: "SlidingTabView", + platforms: [ + .iOS(.v13), .watchOS(.v6), .tvOS(.v13), .macOS(.v10_15) + ], products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. .library( diff --git a/README.md b/README.md index 841f0ca..c2852fc 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,28 @@ Please use Swift Package Manager to install **SlidingTabView** Just instantiate and bind it to your state. That is it! ```swift @State private var selectedTabIndex = 0 -SlidingTabView(selection: $selectedTabIndex,tabs: ["First Tab", "Second Tab"] +SlidingTabView(selection: $selectedTabIndex,tabs: ["First Tab", "Second Tab"]) ``` +If need disabled button tabs: +```swift +@State private var selectedTabIndex = 0 +SlidingTabView(selection: $selectedTabIndex,tabs: ["First Tab", "Second Tab"], isEnabled: false) +``` + +If need change manually a tab: +```swift +@State private var selectedTabIndex = 0 +SlidingTabView( + selectionState:self.$selectedTabIndex, + selection: $selectedTabIndex, + tabs: ["First Tab", "Second Tab"], isEnabled: false +) +``` + +To change the tab, change the value of selectionState and selection + + ## Canvas Preview ```swift struct SlidingTabConsumerView : View { diff --git a/Sources/SlidingTabView/SlidingTabView.swift b/Sources/SlidingTabView/SlidingTabView.swift index cfd54f6..a67411e 100644 --- a/Sources/SlidingTabView/SlidingTabView.swift +++ b/Sources/SlidingTabView/SlidingTabView.swift @@ -30,7 +30,7 @@ public struct SlidingTabView : View { // MARK: Internal State /// Internal state to keep track of the selection index - @State private var selectionState: Int = 0 { + @Binding var selectionState:Int{ didSet { selection = selectionState } @@ -76,20 +76,28 @@ public struct SlidingTabView : View { /// The height of the selection bar background let selectionBarBackgroundHeight: CGFloat + // Enabled / Disabled buttons + let isEnabled:Bool + // MARK: init - public init(selection: Binding, - tabs: [String], - font: Font = .body, - animation: Animation = .spring(), - activeAccentColor: Color = .blue, - inactiveAccentColor: Color = Color.black.opacity(0.4), - selectionBarColor: Color = .blue, - inactiveTabColor: Color = .clear, - activeTabColor: Color = .clear, - selectionBarHeight: CGFloat = 2, - selectionBarBackgroundColor: Color = Color.gray.opacity(0.2), - selectionBarBackgroundHeight: CGFloat = 1) { + public init( + selectionState : Binding = .constant(0), + selection: Binding = .constant(0), + tabs: [String], + font: Font = .body, + animation: Animation = .spring(), + activeAccentColor: Color = .blue, + inactiveAccentColor: Color = Color.black.opacity(0.4), + selectionBarColor: Color = .blue, + inactiveTabColor: Color = .clear, + activeTabColor: Color = .clear, + selectionBarHeight: CGFloat = 2, + selectionBarBackgroundColor: Color = Color.gray.opacity(0.2), + selectionBarBackgroundHeight: CGFloat = 1, + isEnabled:Bool = true + ) { + self._selectionState = selectionState self._selection = selection self.tabs = tabs self.font = font @@ -102,6 +110,7 @@ public struct SlidingTabView : View { self.selectionBarHeight = selectionBarHeight self.selectionBarBackgroundColor = selectionBarBackgroundColor self.selectionBarBackgroundHeight = selectionBarBackgroundHeight + self.isEnabled = isEnabled } // MARK: View Construction @@ -112,14 +121,17 @@ public struct SlidingTabView : View { return VStack(alignment: .leading, spacing: 0) { HStack(spacing: 0) { ForEach(self.tabs, id:\.self) { tab in - Button(action: { - let selection = self.tabs.firstIndex(of: tab) ?? 0 - self.selectionState = selection - }) { - HStack { - Spacer() - Text(tab).font(self.font) - Spacer() + Group{ + if self.isEnabled{ + Button(action: { + let selection = self.tabs.firstIndex(of: tab) ?? 0 + self.selectionState = selection + + }) { + self.getViewTab(tab:tab) + } + }else{ + self.getViewTab(tab:tab) } } .padding(.vertical, 16) @@ -162,6 +174,16 @@ public struct SlidingTabView : View { private func tabWidth(from totalWidth: CGFloat) -> CGFloat { return totalWidth / CGFloat(tabs.count) } + + private func getViewTab(tab:String) -> AnyView{ + return AnyView( + HStack { + Spacer() + Text(tab).font(self.font) + Spacer() + } + ) + } } #if DEBUG @@ -191,4 +213,4 @@ struct SlidingTabView_Previews : PreviewProvider { SlidingTabConsumerView() } } -#endif +#endif \ No newline at end of file