Skip to content

Commit 1d6634f

Browse files
author
Mark Pospesel
authored
[Issue 16] Fix corner radius bug (#17)
1 parent 69aa5ac commit 1d6634f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

Sources/YBottomSheet/BottomSheetController.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public class BottomSheetController: UIViewController {
4646
let view = UIView()
4747
view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
4848
view.backgroundColor = .systemBackground
49-
view.backgroundColor = .systemBackground
5049
return view
5150
}()
5251
/// Bottom sheet drag indicator view.
@@ -178,6 +177,16 @@ private extension BottomSheetController {
178177
func build(_ subview: UIView, title: String) {
179178
contentView.addSubview(subview)
180179
subview.constrainEdges()
180+
181+
if let backgroundColor = subview.backgroundColor,
182+
backgroundColor.rgbaComponents.alpha == 1 {
183+
// use the subview's background color for the sheet
184+
sheetView.backgroundColor = backgroundColor
185+
// but we have to set the subview's background to nil or else
186+
// it will overflow the sheet and not be cropped by the corner radius.
187+
subview.backgroundColor = nil
188+
}
189+
181190
indicatorView = DragIndicatorView(appearance: appearance.indicatorAppearance ?? .default)
182191
indicatorContainer.addSubview(indicatorView)
183192

Tests/YBottomSheetTests/BottomSheetControllerTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,51 @@ final class BottomSheetControllerTests: XCTestCase {
343343
sut.simulateTapCloseButton()
344344
XCTAssertTrue(sut.isDismissed)
345345
}
346+
347+
func test_backgroundColor_copiedFromChild() {
348+
let color: UIColor = .systemPurple
349+
let view = UIView()
350+
view.backgroundColor = color
351+
let sut = makeSUT(view: view)
352+
let traits = UITraitCollection(preferredContentSizeCategory: .large)
353+
XCTAssertNotNil(view.backgroundColor)
354+
355+
sut.loadViewIfNeeded()
356+
357+
XCTAssertEqual(
358+
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
359+
color.resolvedColor(with: traits)
360+
)
361+
XCTAssertNil(view.backgroundColor)
362+
}
363+
364+
func test_clearBackgroundColor_notCopiedFromChild() {
365+
let view = UIView()
366+
view.backgroundColor = .clear
367+
let sut = makeSUT(view: view)
368+
let traits = UITraitCollection(preferredContentSizeCategory: .large)
369+
370+
sut.loadViewIfNeeded()
371+
372+
XCTAssertEqual(
373+
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
374+
UIColor.systemBackground.resolvedColor(with: traits)
375+
)
376+
}
377+
378+
func test_nilBackgroundColor_notCopiedFromChild() {
379+
let view = UIView()
380+
view.backgroundColor = nil
381+
let sut = makeSUT(view: view)
382+
let traits = UITraitCollection(preferredContentSizeCategory: .large)
383+
384+
sut.loadViewIfNeeded()
385+
386+
XCTAssertEqual(
387+
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
388+
UIColor.systemBackground.resolvedColor(with: traits)
389+
)
390+
}
346391
}
347392

348393
private extension BottomSheetControllerTests {

0 commit comments

Comments
 (0)