我有一个带有自定义单元格的UITableView。每个单元格的结构都是这样的:我有contentView,在这个contentView中有backView (白色背景和角半径为16.0的简单的UIView ),在这个backView中,我有一个带有一些图片的imageView。
我想要的是让这个imageView走投无路(在他的父母UIView - backView- borders中)。而且它不是这样工作的。
代码非常简单(来自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函数中更新。
而且它是有效的,除非这个图像没有被逼到角落(而且我相信它应该是)。
(不幸的是,我不可能将cornerRadius设置为UIImageView )。
更新:找到了解决方案。在所有父视图中,我似乎必须直接将'clipsToBounds‘设置为true (在我的例子中,contentView和backView )。
发布于 2017-07-08 10:45:40
您应该设置更高级别容器视图的clipsToBounds属性(如单元格的contentView )。
发布于 2017-07-08 10:44:09
应用imageView.layer.maskToBounds = YES;
将此应用于要设置角半径的视图或图像视图。
正如您所提到的视图的拐角半径,您需要为您的视图设置此参数。
https://stackoverflow.com/questions/44985252
复制相似问题