我有一个带有自定义单元格的UITableView。每个单元格的结构都是这样的:我有contentView,在这个contentView中我有backView(带有白色背景和角度半径16.0的简单UIView),在这个backView中,我有一个imageView和一些图片。ClipsToBounds在单元格中不起作用
我想要的是让这个图像视图(在他的父母的UIView - backView边框内)。而且它不以这种方式工作。
的代码是(从ImageCell.swift)很简单:
self.backView = UIView()
self.backView.backgroundColor = UIColor.white
self.backView.translatesAutoresizingMaskIntoConstraints = false
self.backView.layer.cornerRadius = 16.0
self.contentView.addSubview(backView)
self.picture = UIImageView()
self.picture.translatesAutoresizingMaskIntoConstraints = false
self.picture.contentMode = UIViewContentMode.scaleAspectFill
self.picture.backgroundColor = UIColor.gray
self.picture.clipsToBounds = true
self.backView.addSubview(picture)
let constraintPicTop = NSLayoutConstraint(item: picture, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .topMargin, multiplier: 1.0, constant: -6)
let constraintPicLeft = NSLayoutConstraint(item: picture, attribute: .left, relatedBy: .equal, toItem: backView, attribute: .leftMargin, multiplier: 1.0, constant: -8)
let constraintPicRight = NSLayoutConstraint(item: picture, attribute: .right, relatedBy: .equal, toItem: backView, attribute: .rightMargin, multiplier: 1.0, constant: 8)
constraintBottomPic = NSLayoutConstraint(item: picture, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: contentView, attribute: .topMargin, multiplier: 1.0, constant: 150)
我不知道图像的大小提前,所以constraintBottomPic值在cellForRowAt功能更新。
它的工作,除了这个形象没有角逐(我相信它应该是)。
(不幸的是,我无法为UIImageView设置cornerRadius)。
更新:找到解决方案。看来我必须直接在所有父视图中设置“clipsToBounds”为true(contentView和backView,在我的情况下)。
clipsToBounds应该为图像视图的父视图添加self.backView.clipsToBounds = true。希望能帮助到你。 – luckyShubhra