Skip to content

Commit

Permalink
[Issue 16] Fix corner radius bug (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pospesel authored Mar 29, 2023
1 parent 69aa5ac commit 1d6634f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/YBottomSheet/BottomSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class BottomSheetController: UIViewController {
let view = UIView()
view.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
view.backgroundColor = .systemBackground
view.backgroundColor = .systemBackground
return view
}()
/// Bottom sheet drag indicator view.
Expand Down Expand Up @@ -178,6 +177,16 @@ private extension BottomSheetController {
func build(_ subview: UIView, title: String) {
contentView.addSubview(subview)
subview.constrainEdges()

if let backgroundColor = subview.backgroundColor,
backgroundColor.rgbaComponents.alpha == 1 {
// use the subview's background color for the sheet
sheetView.backgroundColor = backgroundColor
// but we have to set the subview's background to nil or else
// it will overflow the sheet and not be cropped by the corner radius.
subview.backgroundColor = nil
}

indicatorView = DragIndicatorView(appearance: appearance.indicatorAppearance ?? .default)
indicatorContainer.addSubview(indicatorView)

Expand Down
45 changes: 45 additions & 0 deletions Tests/YBottomSheetTests/BottomSheetControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,51 @@ final class BottomSheetControllerTests: XCTestCase {
sut.simulateTapCloseButton()
XCTAssertTrue(sut.isDismissed)
}

func test_backgroundColor_copiedFromChild() {
let color: UIColor = .systemPurple
let view = UIView()
view.backgroundColor = color
let sut = makeSUT(view: view)
let traits = UITraitCollection(preferredContentSizeCategory: .large)
XCTAssertNotNil(view.backgroundColor)

sut.loadViewIfNeeded()

XCTAssertEqual(
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
color.resolvedColor(with: traits)
)
XCTAssertNil(view.backgroundColor)
}

func test_clearBackgroundColor_notCopiedFromChild() {
let view = UIView()
view.backgroundColor = .clear
let sut = makeSUT(view: view)
let traits = UITraitCollection(preferredContentSizeCategory: .large)

sut.loadViewIfNeeded()

XCTAssertEqual(
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
UIColor.systemBackground.resolvedColor(with: traits)
)
}

func test_nilBackgroundColor_notCopiedFromChild() {
let view = UIView()
view.backgroundColor = nil
let sut = makeSUT(view: view)
let traits = UITraitCollection(preferredContentSizeCategory: .large)

sut.loadViewIfNeeded()

XCTAssertEqual(
sut.sheetView.backgroundColor?.resolvedColor(with: traits),
UIColor.systemBackground.resolvedColor(with: traits)
)
}
}

private extension BottomSheetControllerTests {
Expand Down

0 comments on commit 1d6634f

Please sign in to comment.