ios - UICollectionView Cell + UiLabel with AutoLayout

标签 ios iphone uicollectionview autolayout uicollectionviewcell

我正在尝试将 UILabel 固定到它的父单元格。我添加了四个约束(顶部、前导、尾随、底部)这在 iOS 8.0 上运行良好但在 iOS 7.X 上运行不正常。请看下图:

[Click here for full size]

enter image description here

我做错了什么?请指教!

编辑#1

它似乎是从 Xcode 6 GM 之后才坏掉的。我的方法在 Xcode 6 beta 7 中运行良好。

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

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) 
(
    "<NSLayoutConstraint:0x799573a0 H:|-(8)-[UIView:0x798a86e0]   (Names: '|':UIView:0x798ae5d0 )>",
    "<NSLayoutConstraint:0x799573d0 H:[UIView:0x798a86e0]-(43)-|   (Names: '|':UIView:0x798ae5d0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x798a8b00 h=--& v=--& H:[UIView:0x798ae5d0(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x799573d0 H:[UIView:0x798a86e0]-(43)-|   (Names: '|':UIView:0x798ae5d0 )>

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

最佳答案

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

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

关于ios - UICollectionView Cell + UiLabel with AutoLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771026/

相关文章:

ios - 如何在测试飞行时自动上传构建?

iphone - 如何更改 UITabBarController 更多按钮的标题?

ios - Swift 3 中点击声音实现的自定义数字键盘

swift - 获取视频数组的缩略图

ios - Xcode 自动布局不断改变我的布局

ios - 我什么时候使用自有属性(property)?

ios - 通过动态输入元素上传图像不适用于 iOS

iphone - Sybase ULtralite DB 和 iOS 核心数据

ios - UITabBarItem 外观抛出 EXC_BAD_ACCESS

ios - UICollectionView 添加按钮到单元格