Skip to content

Commit 0d859c1

Browse files
Mark Pospeselmpospese
authored andcommitted
[Issue-7] Adjust sheet header appearance
1 parent 200ddfa commit 0d859c1

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

Sources/YBottomSheet/BottomSheetController.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class BottomSheetController: UIViewController {
2121
private let minimumTopOffset: CGFloat = 44
2222
private let minimumContentHeight: CGFloat = 88
2323
private var topAnchor: NSLayoutConstraint?
24+
private var indicatorTopAnchor: NSLayoutConstraint?
2425
private var childHeightAnchor: NSLayoutConstraint?
2526
private var panGesture: UIPanGestureRecognizer?
2627
internal lazy var lastYOffset: CGFloat = {
@@ -50,6 +51,8 @@ public class BottomSheetController: UIViewController {
5051
}()
5152
/// Bottom sheet drag indicator view.
5253
public private(set) var indicatorView: DragIndicatorView!
54+
/// Container view for the drag indicator.
55+
internal let indicatorContainer = UIView()
5356
/// Bottom sheet header view.
5457
public private(set) var headerView: SheetHeaderView!
5558
/// Holds the sheet's child content (view or view controller).
@@ -167,6 +170,8 @@ private extension BottomSheetController {
167170
contentView.addSubview(subview)
168171
subview.constrainEdges()
169172
indicatorView = DragIndicatorView(appearance: appearance.indicatorAppearance ?? .default)
173+
indicatorContainer.addSubview(indicatorView)
174+
170175
headerView = SheetHeaderView(title: title, appearance: appearance.headerAppearance ?? .default)
171176
headerView.delegate = self
172177
buildSheet()
@@ -190,7 +195,7 @@ private extension BottomSheetController {
190195
view.addSubview(dimmerView)
191196
view.addSubview(sheetView)
192197
sheetView.addSubview(stackView)
193-
stackView.addArrangedSubview(indicatorView)
198+
stackView.addArrangedSubview(indicatorContainer)
194199
stackView.addArrangedSubview(headerView)
195200
stackView.addArrangedSubview(contentView)
196201
}
@@ -206,11 +211,14 @@ private extension BottomSheetController {
206211
constant: minimumTopOffset
207212
)
208213

209-
stackView.constrain(
214+
indicatorView.constrain(.bottomAnchor, to: indicatorContainer.bottomAnchor)
215+
indicatorTopAnchor = indicatorView.constrain(
210216
.topAnchor,
211-
to: sheetView.topAnchor,
212-
constant: appearance.layout.cornerRadius - (appearance.indicatorAppearance?.layout.size.height ?? 0)
217+
to: indicatorContainer.topAnchor
213218
)
219+
indicatorView.constrainCenter(.x)
220+
221+
stackView.constrain(.topAnchor, to: sheetView.topAnchor)
214222
stackView.constrainEdges(.horizontal, to: view.safeAreaLayoutGuide)
215223
stackView.constrainEdges(.bottom, to: view.safeAreaLayoutGuide, relatedBy: .greaterThanOrEqual)
216224
stackView.constrainEdges(.bottom, to: view.safeAreaLayoutGuide, priority: Priorities.sheetContentHugging)
@@ -230,9 +238,10 @@ private extension BottomSheetController {
230238
}
231239

232240
func updateIndicatorView() {
233-
indicatorView.isHidden = !isResizable
241+
indicatorContainer.isHidden = !isResizable
234242
if let indicatorAppearance = appearance.indicatorAppearance {
235243
indicatorView.appearance = indicatorAppearance
244+
indicatorTopAnchor?.constant = indicatorAppearance.layout.topInset
236245
if panGesture == nil {
237246
let pan = UIPanGestureRecognizer()
238247
pan.addTarget(self, action: #selector(handlePan))

Sources/YBottomSheet/DragIndicatorView/DragIndicatorView.Appearance+Layout.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,24 @@ extension DragIndicatorView.Appearance {
1515
public var cornerRadius: CGFloat
1616
/// Size for the drag indicator. Default is {60, 4}.
1717
public var size: CGSize
18-
18+
/// Inset of indicator from top of sheet. Default is `8.0`
19+
public var topInset: CGFloat
1920
/// Default layout.
20-
public static var `default` = Layout(cornerRadius: 2.0, size: CGSize(width: 60, height: 4))
21+
public static var `default` = Layout()
2122

2223
/// Initializes a `Layout`.
2324
/// - Parameters:
2425
/// - cornerRadius: corner radius of drag indicator.
2526
/// - size: size for the drag indicator.
27+
/// - topInset: inset of indicator from top of sheet.
2628
public init(
27-
cornerRadius: CGFloat,
28-
size: CGSize
29+
cornerRadius: CGFloat = 2,
30+
size: CGSize = CGSize(width: 60, height: 4),
31+
topInset: CGFloat = 8
2932
) {
3033
self.cornerRadius = cornerRadius
3134
self.size = size
35+
self.topInset = topInset
3236
}
3337
}
3438
}

Tests/YBottomSheetTests/BottomSheetControllerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class BottomSheetControllerTests: XCTestCase {
6161
XCTAssertFalse(sut.isResizable)
6262
XCTAssertFalse(sut.hasHeader)
6363
XCTAssertTrue(sut.headerView.isHidden)
64-
XCTAssertTrue(sut.indicatorView.isHidden)
64+
XCTAssertTrue(sut.indicatorContainer.isHidden)
6565
XCTAssertEqual(sut.appearance.layout.cornerRadius, radius)
6666
XCTAssertEqual(sut.dimmerView.backgroundColor, appearance.dimmerColor)
6767
}
@@ -127,7 +127,7 @@ final class BottomSheetControllerTests: XCTestCase {
127127
indicatorAppearance: nil
128128
)
129129

130-
XCTAssertTrue(sut.indicatorView.isHidden)
130+
XCTAssertTrue(sut.indicatorContainer.isHidden)
131131
XCTAssertFalse(sut.isResizable)
132132
}
133133

Tests/YBottomSheetTests/DragIndicatorView/DragIndicatorViewAppearanceLayoutTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ final class DragIndicatorViewAppearanceLayoutTests: XCTestCase {
1414
let sut = DragIndicatorView.Appearance.Layout.default
1515
XCTAssertEqual(sut.cornerRadius, 2)
1616
XCTAssertEqual(sut.size, CGSize(width: 60, height: 4))
17+
XCTAssertEqual(sut.topInset, 8)
1718
}
1819

19-
func test_propertiesWithRandomValues() {
20+
func test_propertiesWithCustomValues() {
2021
let sut = DragIndicatorView.Appearance.Layout(
2122
cornerRadius: 8,
22-
size: CGSize(width: 100, height: 16)
23+
size: CGSize(width: 100, height: 16),
24+
topInset: 24
2325
)
2426
XCTAssertEqual(sut.cornerRadius, 8)
2527
XCTAssertEqual(sut.size, CGSize(width: 100, height: 16))
28+
XCTAssertEqual(sut.topInset, 24)
2629
}
2730
}

Tests/YBottomSheetTests/DragIndicatorView/DragIndicatorViewTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,26 @@ final class DragIndicatorViewTests: XCTestCase {
2222
XCTAssertEqual(sut.appearance.color, .tertiaryLabel)
2323
XCTAssertEqual(sut.appearance.layout.cornerRadius, 2)
2424
XCTAssertEqual(sut.appearance.layout.size, CGSize(width: 60, height: 4))
25+
XCTAssertEqual(sut.appearance.layout.topInset, 8)
2526
XCTAssertEqual(sut.intrinsicContentSize, CGSize(width: 60, height: 4))
2627
}
2728

28-
func test_init_withRandomValues() {
29+
func test_init_withCustomValues() {
2930
let size = CGSize(width: 100, height: 20)
3031
let appearance = DragIndicatorView.Appearance(
3132
color: .red,
3233
layout: DragIndicatorView.Appearance.Layout(
3334
cornerRadius: 10,
34-
size: size
35+
size: size,
36+
topInset: 12
3537
)
3638
)
3739
let sut = makeSUT(appearance: appearance)
3840
XCTAssertNotNil(sut)
3941
XCTAssertEqual(sut.appearance.color, .red)
4042
XCTAssertEqual(sut.appearance.layout.cornerRadius, 10)
4143
XCTAssertEqual(sut.appearance.layout.size, size)
44+
XCTAssertEqual(sut.appearance.layout.topInset, 12)
4245
XCTAssertEqual(sut.intrinsicContentSize, size)
4346
}
4447

@@ -57,16 +60,18 @@ final class DragIndicatorViewTests: XCTestCase {
5760
let sut = makeSUT()
5861
let size = CGSize(width: 100, height: 8)
5962
let radius: CGFloat = 4
63+
let topInset: CGFloat = 10
6064

6165
XCTAssertNotEqual(sut.appearance.layout.cornerRadius, radius)
6266
XCTAssertNotEqual(sut.appearance.layout.size, size)
6367

6468
sut.appearance = DragIndicatorView.Appearance(
65-
layout: DragIndicatorView.Appearance.Layout(cornerRadius: radius, size: size)
69+
layout: DragIndicatorView.Appearance.Layout(cornerRadius: radius, size: size, topInset: 10)
6670
)
6771

6872
XCTAssertEqual(sut.appearance.layout.cornerRadius, radius)
6973
XCTAssertEqual(sut.appearance.layout.size, size)
74+
XCTAssertEqual(sut.appearance.layout.topInset, topInset)
7075
}
7176
}
7277

0 commit comments

Comments
 (0)