gpt4 book ai didi

ios - Swift 5 组合布局 - itemSize 和 groupSize 扭曲项目 subview

转载 作者:行者123 更新时间:2023-11-30 10:27:10 24 4
gpt4 key购买 nike

我正在为我的collectionView使用CompositionalLayout来在我的应用程序的某些detailView中创建水平 ScrollView 。我需要这些项目来保存圆形 ImageView ,我计划在其中显示用户的个人资料图片。我创建了一个自定义类来表示将位于 collectionView 的此特定部分中的组中的项目。我知道我正确地舍入了我的 customCell 内的 imageView,但问题是单元格内的 imageView 对于组中的每个不同项目以一种奇怪的方式扭曲 - 我每次更改 itemSize 的值时都会注意到groupSize imageViews 以另一种形式扭曲。

我想要实现的目标: enter image description here

我得到了什么:

enter image description here

这是我的 CollaboratorsLayoutSection 代码:

func generateCollaboratorsLayout(isWide: Bool) -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(120),
heightDimension: .absolute(160))

let item = NSCollectionLayoutItem(layoutSize: itemSize)

let groupSize = NSCollectionLayoutSize(
widthDimension: .absolute(150),
heightDimension: .absolute(120))
let group = NSCollectionLayoutGroup.vertical(
layoutSize: groupSize,
subitem: item,
count: 1)
group.contentInsets = NSDirectionalEdgeInsets(
top: 5,
leading: 5,
bottom: 5,
trailing: 5)

let headerSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0), heightDimension: .estimated(44))
let sectionHeader = NSCollectionLayoutBoundarySupplementaryItem(
layoutSize: headerSize,
elementKind: ProjectDetailViewController.sectionHeaderElementKind, alignment: .top)

let section = NSCollectionLayoutSection(group: group)
section.boundarySupplementaryItems = [sectionHeader]
section.orthogonalScrollingBehavior = .groupPaging

return section
}

这是我用于项目的自定义单元:

class CollaboratorsCell: UICollectionViewCell {
static let reuseIdentifier: String = "CollaboratorsCell"

// MARK: UILabel properties on each collaborators
let usernameLabel = UILabel()
let roleLabel = UILabel()
let memberPhotoView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let contentContainer = UIView()

override init(frame: CGRect) {
super.init(frame: frame)

}

func configureCollaboratorCell(with project: Project, indexPath: IndexPath) {
contentContainer.translatesAutoresizingMaskIntoConstraints = false

contentView.addSubview(memberPhotoView)
contentView.addSubview(contentContainer)
contentContainer.backgroundColor = .blue

memberPhotoView.translatesAutoresizingMaskIntoConstraints = false
memberPhotoView.backgroundColor = .green
memberPhotoView.layer.masksToBounds = false
memberPhotoView.layer.cornerRadius = memberPhotoView.bounds.width / 2
memberPhotoView.clipsToBounds = true
contentContainer.addSubview(memberPhotoView)

usernameLabel.translatesAutoresizingMaskIntoConstraints = false
usernameLabel.text = project.members[indexPath.row]
usernameLabel.textColor = .white
usernameLabel.font = UIFont(name: "Avenir-Medium", size: 14)
usernameLabel.adjustsFontForContentSizeCategory = true
contentContainer.addSubview(usernameLabel)

roleLabel.translatesAutoresizingMaskIntoConstraints = false
roleLabel.text = "ROLE: "
roleLabel.textColor = .white
roleLabel.font = UIFont(name: "Avenir", size: 12)
roleLabel.adjustsFontForContentSizeCategory = true
contentContainer.addSubview(roleLabel)

let spacing = CGFloat(10)
NSLayoutConstraint.activate([
contentContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
contentContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
contentContainer.topAnchor.constraint(equalTo: contentView.topAnchor),
contentContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),

// memberPhotoView.leadingAnchor.constraint(equalTo: contentContainer.leadingAnchor, constant: spacing),
// memberPhotoView.trailingAnchor.constraint(equalTo: contentContainer.trailingAnchor, constant: spacing),
memberPhotoView.topAnchor.constraint(equalTo: contentContainer.topAnchor, constant: 0),
memberPhotoView.centerXAnchor.constraint(equalTo: self.contentContainer.centerXAnchor),
// memberPhotoView.centerYAnchor.constraint(equalTo: self.contentContainer.centerYAnchor),

usernameLabel.topAnchor.constraint(equalTo: memberPhotoView.bottomAnchor, constant: spacing),
usernameLabel.leadingAnchor.constraint(equalTo: memberPhotoView.leadingAnchor),
usernameLabel.trailingAnchor.constraint(equalTo: memberPhotoView.trailingAnchor),

roleLabel.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor),
roleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
roleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
roleLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
}

required init?(coder: NSCoder) {
fatalError("Not happening in CollaborrabotsCell")
}

}

最佳答案

事实证明,问题出在 autoLayout 约束上,一旦我添加了 memberPhotoView 的宽度和高度约束,我就开始为项目获得完美的圆形。

我仍然不确定为什么 compositionalLayout 在没有这些约束的情况下使圆圈变形。

关于ios - Swift 5 组合布局 - itemSize 和 groupSize 扭曲项目 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com