Skip to content

Commit

Permalink
Merge pull request #3 from KazaiMazai/Alignment-issue-fix
Browse files Browse the repository at this point in the history
Alignment issue fix
  • Loading branch information
KazaiMazai authored May 21, 2022
2 parents 754ca0a + ec4c48c commit c3c6396
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ Custom segmented picker for SwiftUI

struct SegmentedPickerExample: View {
let titles: [String]
@State var selectedIndex: Int = 0
@State var selectedIndex: Int?

var body: some View {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 ?? 0 }),
set: { selectedIndex = $0 }),
selectionAlignment: .bottom,
content: { item, isSelected in
Text(item)
.foregroundColor(isSelected ? Color.black : Color.gray )
Expand All @@ -25,11 +26,12 @@ struct SegmentedPickerExample: View {
selection: {
VStack(spacing: 0) {
Spacer()
Rectangle()
.fill(Color.black)
.frame(height: 1)
Color.black.frame(height: 1)
}
})
.onAppear {
selectedIndex = 0
}
.animation(.easeInOut(duration: 0.3))
}
}
Expand All @@ -49,14 +51,14 @@ or this guy with a capsule as selection view:

struct SegmentedPickerExample: View {
let titles: [String]
@State var selectedIndex: Int = 0
@State var selectedIndex: Int?

var body: some View {
SegmentedPicker(
titles,
selectedIndex: Binding(
get: { selectedIndex },
set: { selectedIndex = $0 ?? 0 }),
set: { selectedIndex = $0 }),
content: { item, isSelected in
Text(item)
.foregroundColor(isSelected ? Color.white : Color.gray )
Expand All @@ -67,6 +69,9 @@ struct SegmentedPickerExample: View {
Capsule()
.fill(Color.gray)
})
.onAppear {
selectedIndex = 0
}
.animation(.easeInOut(duration: 0.3))
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/SegmentedPicker/SegmentedPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public struct SegmentedPicker<Element, Content, Selection>: View
private let data: Data
private let selection: () -> Selection
private let content: (Data.Element, Bool) -> Content
private let selectionAlignment: VerticalAlignment

public init(_ data: Data,
selectedIndex: Binding<Data.Index?>,
selectionAlignment: VerticalAlignment = .center,
@ViewBuilder content: @escaping (Data.Element, Bool) -> Content,
@ViewBuilder selection: @escaping () -> Selection) {

Expand All @@ -32,11 +34,12 @@ public struct SegmentedPicker<Element, Content, Selection>: View
self._selectedIndex = selectedIndex
self._frames = State(wrappedValue: Array(repeating: .zero,
count: data.count))
self.selectionAlignment = selectionAlignment
}

public var body: some View {
ZStack(alignment: Alignment(horizontal: .horizontalCenterAlignment,
vertical: .center)) {
vertical: selectionAlignment)) {

if let selectedIndex = selectedIndex {
selection()
Expand Down

0 comments on commit c3c6396

Please sign in to comment.