我正在尝试制作此屏幕
,我将 UICollectionViewCompositionalLayout 与自调整单元格一起使用,但由于图像的大小,它不起作用。我尝试改变约束的优先级,但没有任何结果。告诉我如何达到预期的结果?
失败
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
("<NSAutoresizingMaskLayoutConstraint:0x6000009bde00 h=--& v=--& UIView:0x7fcf14f14240.height == 0 (active)>",
"<NSLayoutConstraint:0x60000098c9b0 V:|-(0)-[UIImageView:0x7fcf14d0bab0] (active, names: '|':UIView:0x7fcf14f14240 )>",
"<NSLayoutConstraint:0x60000098cc30 UIImageView:0x7fcf14d0bab0.height == 225 (active)>",
"<NSLayoutConstraint:0x60000098ce60 V:[UIImageView:0x7fcf14d0bab0]-(15)-[UILabel:0x7fcf14f12220] (active)>",
"<NSLayoutConstraint:0x60000098cf50 UILabel:0x7fcf14f12220.bottom == UIView:0x7fcf14f14240.bottom (active)>")
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000098cc30 UIImageView:0x7fcf14d0bab0.height == 225 (active)>
创建部分
private func createMainSection() -> NSCollectionLayoutSection {
let size = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .estimated(225))
let item = NSCollectionLayoutItem(layoutSize: size)
let group = NSCollectionLayoutGroup.vertical(layoutSize: size, subitem: item, count: 1)
let section = NSCollectionLayoutSection(group: group)
section.contentInsets = .init(top: 20, leading: 20, bottom: 20, trailing: 20)
return section
}
单元格布局
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(posterView)
contentView.addSubview(titleLabel)
contentView.addSubview(overviewLabel)
posterView.anchor(
top: contentView.topAnchor,
right: nil,
bottom: nil,
left: contentView.leftAnchor,
width: 150,
height: 225)
titleLabel.anchor(
top: contentView.topAnchor,
right: contentView.rightAnchor,
bottom: nil,
left: posterView.rightAnchor,
paddingLeft: 15)
overviewLabel.anchor(
top: posterView.bottomAnchor,
right: contentView.rightAnchor,
bottom: contentView.bottomAnchor,
left: contentView.leftAnchor,
paddingTop: 15)
}
锚函数
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
topAnchor.constraint(equalTo: top, constant: paddingTop + topInset).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom - bottomInset).isActive = true
}
if let left = left {
leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}