Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
66 changes: 44 additions & 22 deletions Sources/SlidingTabView/SlidingTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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<Int>,
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<Int> = .constant(0),
selection: Binding<Int> = .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
Expand All @@ -102,6 +110,7 @@ public struct SlidingTabView : View {
self.selectionBarHeight = selectionBarHeight
self.selectionBarBackgroundColor = selectionBarBackgroundColor
self.selectionBarBackgroundHeight = selectionBarBackgroundHeight
self.isEnabled = isEnabled
}

// MARK: View Construction
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -191,4 +213,4 @@ struct SlidingTabView_Previews : PreviewProvider {
SlidingTabConsumerView()
}
}
#endif
#endif