我有一个带有自定义单元格的UITableView。每个单元的结构是这样的:我有contentView,在这个contentView中,我有backView(带有白色背景和圆角半径为16.0的简单UIView),在这个backView中,我有一个带图片的imageView。
我想要的是将这个imageView转角(在他的父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)
我事先不知道图像的大小,因此在cellForRowAt函数中constraintBottomPic值正在更新。
它可以正常工作,只不过该图像没有弯曲(我相信应该是)。
(不幸的是,我无法为UIImageView设置cornerRadius)。
更新:找到了解决方案。看来我必须直接在所有父视图中将“ clipsToBounds”设置为true(在我的情况下为contentView和backView)。
您应该设置clipsToBounds
高级容器视图的属性(如contentView
单元格的)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句