diff --git a/Sources/YBottomSheet/BottomSheetController.swift b/Sources/YBottomSheet/BottomSheetController.swift index f9f08c7..03f916d 100644 --- a/Sources/YBottomSheet/BottomSheetController.swift +++ b/Sources/YBottomSheet/BottomSheetController.swift @@ -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. @@ -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) diff --git a/Tests/YBottomSheetTests/BottomSheetControllerTests.swift b/Tests/YBottomSheetTests/BottomSheetControllerTests.swift index 5bfbd35..fcbdb02 100644 --- a/Tests/YBottomSheetTests/BottomSheetControllerTests.swift +++ b/Tests/YBottomSheetTests/BottomSheetControllerTests.swift @@ -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 {