简体   繁体   中英

UIView clipsToBounds doesn't work

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.

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

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
}

I defined my view (UIView in my case) like that:

fileprivate let backView: UIView = {
    let view = UIView()
    view.clipsToBounds = true
    view.layer.masksToBounds = false
    view.layer.cornerRadius = 10
    view.translatesAutoresizingMaskIntoConstraints = false
    return view
}()

The image:

fileprivate let imgView: UIImageView = {
    let iv = UIImageView()
    iv.translatesAutoresizingMaskIntoConstraints = false
    return iv
}()

In my case I defined these elements in a custom table view cell:

class customCell: UITableViewCell {

Although I set "clipsToBounds = true" in the definition of background view, not clip the image.

But, if I set "clipsToBounds = true" later, it clips the image.

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    backView.addSubview(imgView)
    contentView.addSubview(backView)

    imgView.topAnchor.constraint(equalTo: backView.topAnchor, constant: 0).isActive = true
    imgView.leadingAnchor.constraint(equalTo: backView.leadingAnchor, constant: 0).isActive = true
    imgView.trailingAnchor.constraint(equalTo: backView.trailingAnchor, constant: 0).isActive = true
    imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor, multiplier: 1/4).isActive = true

    backView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
    backView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 15).isActive = true
    backView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -15).isActive = true
    backView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

And inside "init" method:

backView.clipsToBounds = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM