我正在尝试创建自定义UICollectionViewCell。 我给单元格设置了屏幕本身的高度和宽度:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width * 0.95
let height = UIScreen.main.bounds.height * 0.5
return CGSize(width: width, height: height)
}
因此,没有固定的数字可以导致这种情况。 我的自定义单元格包含3个UILabel和3个UIImageviews。 我将2个标签和2个图像放在堆栈中,将其他2个放在堆栈中,然后将其中2个放在堆栈中。 这是代码:
func setupStacks(){
dislikeImage.frame.size.width = likeImage.frame.width
dislikeImage.frame.size.height = likeImage.frame.height
let likesStackView = UIStackView(arrangedSubviews: [likeImage, numberOfLikes, dislikeImage, numberOfDislikes])
likesStackView.axis = .horizontal
likesStackView.distribution = .fill
likesStackView.translatesAutoresizingMaskIntoConstraints = false
let titleAndImageStack = UIStackView(arrangedSubviews: [postTitle, postImage])
titleAndImageStack.axis = .vertical
titleAndImageStack.distribution = .fill
titleAndImageStack.spacing = 8
titleAndImageStack.translatesAutoresizingMaskIntoConstraints = false
let stack = UIStackView(arrangedSubviews: [titleAndImageStack, likesStackView])
stack.axis = .vertical
stack.distribution = .fillProportionally
stack.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(stack)
NSLayoutConstraint.activate([
likesStackView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor),
likesStackView.widthAnchor.constraint(lessThanOrEqualTo: self.contentView.widthAnchor, multiplier: 0.4),
stack.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 8),
stack.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -8),
stack.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor),
// stack.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant: 1),
// stack.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant: -1)
stack.leadingAnchor.constraint(equalToSystemSpacingAfter: self.contentView.leadingAnchor, multiplier: 0.98),
stack.trailingAnchor.constraint(equalToSystemSpacingAfter: self.contentView.trailingAnchor, multiplier: -0.98)
])
}
问题在于它在非iPhone X iPhone上看起来很糟糕。 这是一张图片,例如:
我尝试过,但是找不到问题以及为什么发生这种情况。
编辑:更清楚地说,我希望它看起来尽可能接近iPhone Xs模拟器,我尝试更改titleAndImage堆栈的宽度以及尾随和前导锚点,但是没有用。问题是,我将titleAndImageStack.spacing设置得尽可能少,标题消失了,但是图像向侧面拉开了,我该如何解决?
答案 0 :(得分:0)
问题在于X型iPhone的长宽比与旧版iPhone不同。 我通过根据屏幕更改单元格大小来修复它,这不是修复它的最优雅的方法,但是它可以工作。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let screenHeight = UIScreen.main.nativeBounds.height
var width: CGFloat!
var height: CGFloat!
//iPhone X and above.
if screenHeight > 2208{
width = UIScreen.main.bounds.width * 0.95
height = UIScreen.main.bounds.height * 0.5
}
//iPhone Xr
else if screenHeight == 1792{
width = UIScreen.main.bounds.width * 0.85
height = UIScreen.main.bounds.height * 0.45
}
//iPhone 8+ and below
else{
width = UIScreen.main.bounds.width * 0.85
height = UIScreen.main.bounds.height * 0.55
}
return CGSize(width: width, height: height)
}