具有AutoLayout的UICollectionView Cell + UiLabel

 sdx3418153 发布于 2022-12-21 20:47

我正在尝试将UILabel固定到它的父细胞上.我添加了四个约束(顶部,前导,尾随,底部),它们在iOS 8.0上运行良好,但在iOS 7.X上运行不正常.请看下面的图片:

[点击这里查看完整尺寸]

在此输入图像描述

我究竟做错了什么?请指教!

编辑#1

它似乎只是在Xcode 6 GM之后才被打破.我的方法在Xcode 6 beta 7中运行良好.

此外,如果我减少内部视图的宽度,它会抛出以下警告:

2014-09-10 19:58:28.109 Test[57827:60b] 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) 
(
    "",
    "",
    ""
)

Will attempt to recover by breaking constraint 


Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in  may also be helpful.

Attila H.. 21

覆盖自定义单元格layoutSubviews是一种可能的解决方法:

override func layoutSubviews() {
    contentView.frame = bounds
    super.layoutSubviews()
}

哇,神奇地删除了我的幽灵约束:``.你能解释一下这是怎么解决的吗?假设这是因为单元格的contentView带有默认边距(距其外观为8px),我是否正确? (2认同)


coco.. 6

UIView:0x798ae5d0是CollectionViewCell的contentView.不知何故,它在某个时刻使用UICollectionViewCells defaultSize,即(50.0,50.0).


由于水平边距8 + 43 = 51大于contentView(50),因此无法满足布局.



可以使布局更灵活,因此它也适用于(50.0,50.0)尺寸.在您的情况下,通过将等于43更改为<= 43或通过将优先级降低到1000到750或999.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有