【发布时间】:2019-12-04 06:25:58
【问题描述】:
UIImage 正在集合视图单元之外展开。当我向单元格添加阴影时,问题就开始了。
UI 是在代码中完成的,特别是当我将单元格上的 maskToBounds 设置为 false 时向单元格添加阴影时,图像会扩展到单元格边界之外。
static let identifier = "kParameterCollectionViewCell"
let bgView = UIView()
let imageView = UIImageView()
let nameLabel = UILabel()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.clipsToBounds = true
self.autoresizesSubviews = true
self.backgroundColor = UIColor.clear
self.layer.cornerRadius = 20.0
// Add Shadows
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowRadius = 1.0
self.layer.shadowOpacity = 1.0
self.layer.shadowOffset = CGSize(width: 5.0, height: 5.0)
self.layer.masksToBounds = false
bgView.frame = self.bounds
bgView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
bgView.backgroundColor = .clear
imageView.translatesAutoresizingMaskIntoConstraints = false
//imageView.backgroundColor = UIColor.clear
imageView.layer.cornerRadius = 2.0
imageView.layer.borderColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
imageView.backgroundColor = UIColor.clear
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.layer.masksToBounds = true
self.addSubview(imageView)
我希望图像视图位于单元格的最右侧。
我会尝试发布两张照片,第一张是正在发生的事情,第二张是我所期望的。
【问题讨论】:
-
你在哪里为这些子视图添加约束,特别是
imageView?您将translatesAutoresizingMaskIntoConstraints设置为false,但似乎从未设置它应该使用的所需约束... -
您是否使用了视图调试器?它可以帮助您调试视图。
-
masksToBounds 不能与 clipsToBounds 一起使用,您的影子在外面。为您的 imageView 创建容器并对其进行剪辑。
标签: ios swift uicollectionview uiimageview