【发布时间】:2019-02-06 09:39:56
【问题描述】:
我尝试在UICollectionViewCell 中以编程方式查看,我认为将“以编程方式查看代码”放在UICollectionViewCell 中是明智的。
这是我的代码:
override func awakeFromNib() {
super.awakeFromNib()
let view = UIView()
view.frame = CGRect.zero
view.backgroundColor = UIColor.black
self.contentView.addSubview(view)
self.contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
view.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0),
view.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: 0),
view.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: 25),
view.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 25),
])
self.contentView.layoutIfNeeded()
}
我想知道为什么自动布局不起作用,如果我使用手动CGRect,视图确实出现了。
【问题讨论】:
标签: ios swift uicollectionview autolayout uicollectionviewcell