简体   繁体   English

UIView clipsToBounds 不起作用

[英]UIView clipsToBounds doesn't work

I have a view controller with XIB, with a view ( contentView ) inside.我有一个带有 XIB 的视图控制器,里面有一个视图( contentView )。 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.我在视图控制器的viewDidLoad中设置了角半径和clipsToBounds

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.该应用程序针对 iOS 10 和 11,两者都有相同的问题。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

I found a solution, I move the clipsToBound in the viewDidLayoutSubviews instead viewDidLoad and now works我找到了一个解决方案,我移动了 viewDidLayoutSubviews 中的 clipsToBound 而不是 viewDidLoad 现在可以工作了

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:我像这样定义了我的视图(在我的情况下为 UIView):

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.虽然我在背景视图的定义中设置了“clipsToBounds = true” ,但没有剪辑图像。

But, if I set "clipsToBounds = true" later, it clips the image.但是,如果我稍后设置“clipsToBounds = true” ,它会剪辑图像。

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:“init”方法中:

backView.clipsToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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