UICollectionViewCell自动布局 [英] UICollectionViewCell Auto Layout

查看:95
本文介绍了UICollectionViewCell自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的故事板中编写了一个简单的自定义 UICollectionViewCell ,我也以编程方式完成了相同的结果。

I have made up a simple custom UICollectionViewCell in my storyboard, I also did this programatically with identical results.

它有一个单一的 UIImageView 和两个 UILabels ,如下所示:

It has a singular UIImageView and two UILabels, like so:

          <--UIIMAGEVIEW-->          <--UILABEL-------->
|-(10px)- |               | -(10px)-                     -(10px)-|
          <--------------->          <--UILABEL-------->

图像位于左侧,然后右侧是两个标签,每个标签位于上方和下方其他。

The image is sat on the left, then to the right of it are two labels above and below each other.

每个间隔有10个像素间隙,并且都以单元格Y轴为中心。然后标签有一个常数来偏离中心(向上和向下)。

Each are spaced out with 10 pixel gaps, and all centred on the cells Y axis. The labels then have a constant to offset them from the centre (up and down).

图像的高度和宽度固定为44px,标签的高度固定为20px(具有灵活的宽度)。

The image has fixed height and width of 44px and the labels have a fixed height of 20px (with flexible width).

约束按预期工作并且永远不会破坏应用程序,但它们总是在下面吐出这个恼人的控制台日志信息,因为它们显然正在破坏。

The constraints work as expected and never break the app, but they always spit out this annoying console log info below, as they are apparently breaking.

如果我将此行添加到我的 UICollectionViewCell 子类中,则约束不再中断并吐出控制台错误,但它们在屏幕上完全坏了。

If I add this line to my UICollectionViewCell subclass, the constraints no longer break and spit out console errors, but they are totally broken onscreen.

[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];

任何线索?这似乎是不合逻辑的!

Any clues? It seems so illogical!

以下是控制台错误代码:

Here is the console error code:

2014-10-25 18:28:14.273 Air Plan[1579:580797] 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:0x17009edc0 H:[UIImageView:0x1701f7d00(44)]>",
    "<NSLayoutConstraint:0x17009f130 H:|-(10)-[UIImageView:0x1701f7d00]   (Names: '|':UIView:0x170183810 )>",
    "<NSLayoutConstraint:0x17009f270 H:[UIImageView:0x1701f7d00]-(10)-[UILabel:0x155e59970'detailLbl']>",
    "<NSLayoutConstraint:0x17009f310 H:[UILabel:0x155e59970'detailLbl']-(10)-|   (Names: '|':UIView:0x170183810 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17409eff0 h=--& v=--& H:[UIView:0x170183810(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17009edc0 H:[UIImageView:0x1701f7d00(44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-10-25 18:28:14.276 Air Plan[1579:580797] 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:0x17009fcc0 H:[UIImageView:0x1701fa800(44)]>",
    "<NSLayoutConstraint:0x17009ff40 H:|-(10)-[UIImageView:0x1701fa800]   (Names: '|':UIView:0x170180a90 )>",
    "<NSLayoutConstraint:0x1702800a0 H:[UIImageView:0x1701fa800]-(10)-[UILabel:0x155e5b440'detailLbl']>",
    "<NSLayoutConstraint:0x170280140 H:[UILabel:0x155e5b440'detailLbl']-(10)-|   (Names: '|':UIView:0x170180a90 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17409ea00 h=--& v=--& H:[UIView:0x170180a90(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x17009fcc0 H:[UIImageView:0x1701fa800(44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-10-25 18:28:14.278 Air Plan[1579:580797] 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:0x170280a00 H:[UIImageView:0x1701fab00(44)]>",
    "<NSLayoutConstraint:0x170280c80 H:|-(10)-[UIImageView:0x1701fab00]   (Names: '|':UIView:0x1701822f0 )>",
    "<NSLayoutConstraint:0x170280dc0 H:[UIImageView:0x1701fab00]-(10)-[UILabel:0x155e5cd10'detailLbl']>",
    "<NSLayoutConstraint:0x170280e60 H:[UILabel:0x155e5cd10'detailLbl']-(10)-|   (Names: '|':UIView:0x1701822f0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17409f180 h=--& v=--& H:[UIView:0x1701822f0(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170280a00 H:[UIImageView:0x1701fab00(44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-10-25 18:28:14.280 Air Plan[1579:580797] 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:0x170281090 H:[UIImageView:0x1701fae00(44)]>",
    "<NSLayoutConstraint:0x170281310 H:|-(10)-[UIImageView:0x1701fae00]   (Names: '|':UIView:0x170182220 )>",
    "<NSLayoutConstraint:0x1702813b0 H:[UIImageView:0x1701fae00]-(10)-[UILabel:0x155e5d620'titleLbl']>",
    "<NSLayoutConstraint:0x170281400 H:[UILabel:0x155e5d620'titleLbl']-(10)-|   (Names: '|':UIView:0x170182220 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x17409fd60 h=--& v=--& H:[UIView:0x170182220(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170281090 H:[UIImageView:0x1701fae00(44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2014-10-25 18:28:14.282 Air Plan[1579:580797] 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:0x170281b80 H:[UIImageView:0x1701fb100(44)]>",
    "<NSLayoutConstraint:0x17409f4a0 H:|-(10)-[UIImageView:0x1701fb100]   (Names: '|':UIView:0x170182150 )>",
    "<NSLayoutConstraint:0x17409f5e0 H:[UIImageView:0x1701fb100]-(10)-[UILabel:0x155d64210'detailLbl']>",
    "<NSLayoutConstraint:0x17409f680 H:[UILabel:0x155d64210'detailLbl']-(10)-|   (Names: '|':UIView:0x170182150 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x1742800f0 h=--& v=--& H:[UIView:0x170182150(50)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170281b80 H:[UIImageView:0x1701fb100(44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.


推荐答案

我也遇到了这个问题并通过添加此问题来修复它在UICollectionViewCell子类中:

I faced that problem too and fixed it by adding this inside the UICollectionViewCell subclass:

SWIFT 3

override func awakeFromNib() {
    contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
}

OBJC

- (void)awakeFromNib{
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
}

Xcode 6.1 iOS 8.0.2,8.1

Xcode 6.1 iOS 8.0.2, 8.1

您还可以查看以下答案: UICollectionView Cell + UiLabel with AutoLayout

You could also check this answer: UICollectionView Cell + UiLabel with AutoLayout

这篇关于UICollectionViewCell自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆