xcode UICollectionViewCell 自动布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26565197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UICollectionViewCell Auto Layout
提问by Josh Kahane
I have made up a simple custom UICollectionViewCell
in my storyboard, I also did this programatically with identical results.
我UICollectionViewCell
在我的故事板中创建了一个简单的自定义,我也以相同的结果以编程方式进行了此操作。
It has a singular UIImageView
and two UILabels
, like so:
它有一个单数UIImageView
和两个UILabels
,如下所示:
<--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.
图像位于左侧,然后在其右侧是两个相互上下的标签。
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).
每个都以 10 个像素间隔隔开,并且都以单元格 Y 轴为中心。然后标签有一个常数来从中心(向上和向下)偏移它们。
The image has fixed height and width of 44px and the labels have a fixed height of 20px (with flexible width).
图像的高度和宽度固定为 44 像素,标签的高度固定为 20 像素(具有灵活的宽度)。
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.
约束按预期工作并且永远不会破坏应用程序,但它们总是在下面吐出这个烦人的控制台日志信息,因为它们显然正在破坏。
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.
如果我将这一行添加到我的UICollectionViewCell
子类中,约束不再中断并吐出控制台错误,但它们在屏幕上完全中断。
[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.
回答by Alexander Polovinka
I faced that problem too and fixed it by adding this inside the UICollectionViewCell subclass:
我也遇到了这个问题,并通过在 UICollectionViewCell 子类中添加它来修复它:
SWIFT 3
斯威夫特 3
override func awakeFromNib() {
contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
}
OBJC
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
You could also check this answer: UICollectionView Cell + UiLabel with AutoLayout
回答by user1765464
Right after creation cell and cell's content view are 50x50 (doesn't matter what size is set by collection layout).
创建后单元格和单元格的内容视图为 50x50(与集合布局设置的大小无关)。
Auto layout first time works after cell got correct size but content view is still 50x50. Probably it's iOS 8 issue. Other layout works with correct content view size. So finely you see a correct layout but log output is littered.
单元格大小正确后,自动布局第一次工作,但内容视图仍然是 50x50。可能是 iOS 8 的问题。其他布局适用于正确的内容视图大小。如此精细,您会看到正确的布局,但日志输出却乱七八糟。
Just set in awakeFromNib any frame bigger then minimal cell size to content view for iOS 8.
只需在awakeFromNib 中将任何大于最小单元格大小的框架设置为iOS 8 的内容视图。
In your case
在你的情况下
|-(10px)-UIImageView(44px)-(10px)-UILabel-(10px)-|
minimal cell width is 74 - sum of all explicit constants.
最小单元格宽度为 74 - 所有显式常量的总和。
- (void)awakeFromNib {
[super awakeFromNib];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.) {
self.contentView.frame = CGRectMake(0, 0, 200, 100);
}
}
Had the same issue but setting auoresizingMask did not work. And setting frame resolved the issue.
有同样的问题,但设置 auoresizingMask 不起作用。设置框架解决了这个问题。
回答by Chris Prince
I had this issue as well. Adding:
我也有这个问题。添加:
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
broke the layout of my view. In my case, I had to tweak a couple of layout constraints to get the view back to the way I wanted. In one case an image was not vertically centered because the cell's height was insufficiently constrained. (There, I added a bottom constraint to the lowest element in the cell). In another case, another image (in a different cell) was not centered and I had to make a different constraint change. I added a height constraint on that image, and removed a top constraint and that solved the problem.
打破了我的看法的布局。就我而言,我必须调整一些布局约束才能使视图恢复到我想要的方式。在一种情况下,图像没有垂直居中,因为单元格的高度限制不足。(在那里,我向单元格中的最低元素添加了底部约束)。在另一种情况下,另一个图像(在不同的单元格中)没有居中,我不得不进行不同的约束更改。我在该图像上添加了高度约束,并删除了顶部约束并解决了问题。
I also had problems in both of these cells with UILabel
s that were no longer constrained to the width of the collection view cell. That is, when the text was too long, the text wouldn't trail off nicely with "..." within the cell, it would just run past the end of the cell. In both of these cases, the only work-around I could come up with was to add a fixed-width constraint to the label, thus making the width of the label's non-adaptive. There were previously trailing constraints that had worked prior to setting translatesAutoresizingMaskIntoConstraints
to NO.
我在这两个单元格中也遇到了问题,UILabel
s 不再受集合视图单元格的宽度限制。也就是说,当文本太长时,文本不会在单元格内用“...”很好地拖尾,它只会越过单元格的末尾。在这两种情况下,我能想出的唯一解决方法是向标签添加固定宽度约束,从而使标签的宽度变得非自适应。在设置translatesAutoresizingMaskIntoConstraints
为 NO之前,之前存在有效的尾随约束。