UIView clipsToBounds doesn't work

2019-08-23 09:07发布

问题:

I have a view controller with XIB, with a view (contentView) inside. This view contains some buttons.

The content view has round corners and clips to bounds, but it doesn't respect the clipping rect. I set the corner radius and the clipsToBounds in the viewDidLoad of the view controller.

Here you can see the reveal screenshot that shows that the view is composed in the correct way, but on simulator and device clipping bounds are not respected.

Anybody can please help me to understand what happen.

The app is targeted to iOS 10 and 11, and both have the same issue.

回答1:

I found a solution, I move the clipsToBound in the viewDidLayoutSubviews instead viewDidLoad and now works

override func viewDidLoad() {
    super.viewDidLoad()

    contentView.layer.cornerRadius = Dimensions.CornerRaius
    contentView.dropShadow()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    contentView.clipsToBounds = true
}