UITableViewCell子类的奇怪布局约束
我刚遇到一个关于奇怪的UITableViewCell子类布局的奇怪问题。 我已经在代码中配置了所有这些东西的UITableViewCell
子类。UITableViewCell子类的奇怪布局约束
- iPhone 5S和下部WORK PERFECT
- iPhone 6或加上WRONG :(
我确定这是子类问题。
(lldb) po _tableView
<UITableView: 0x7fa5db054c00; frame = (0 0; 375 618); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fa5d96649e0>; layer = <CALayer: 0x7fa5d96632a0>; contentOffset: {0, 0}; contentSize: {375, 15552}>
代码:
@interface ColorCell : UITableViewCell
@end
@implementation ColorCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.background = [UIView new];
self.top = [UIView new];
self.bottom = [UIView new];
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
self.background.translatesAutoresizingMaskIntoConstraints = NO;
self.top. translatesAutoresizingMaskIntoConstraints = NO;
self.bottom. translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:self.background];
[self.background addSubview:self.top];
[self.background addSubview:self.bottom];
self.top.backgroundColor = [UIColor purpleColor];
self.bottom.backgroundColor = [UIColor blackColor];
}
return self;
}
- (void)updateConstraints
{
[super updateConstraints];
if (!self.didSetupConstraints) {
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_firstColor,_secondColor,_thirdColor,_fourthColor,_fifthColor,_background,_top,_bottom,_title,_stars,_favourites);
NSString *format;
NSArray *constraintsArray;
NSDictionary *metrics = @{@"topHeight":@100.0, @"bottomHeight":@50};
format = @"V:|[_background]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
[self.contentView addConstraints:constraintsArray];
format = @"H:|[_background]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:NSLayoutFormatAlignAllCenterX metrics:nil views:viewsDictionary];
[self.contentView addConstraints:constraintsArray];
format = @"V:|[_top(topHeight)][_bottom(bottomHeight)]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:metrics views:viewsDictionary];
[_background addConstraints:constraintsArray];
format = @"H:|[_bottom]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_background addConstraints:constraintsArray];
format = @"H:|[_top]|";
constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary];
[_background addConstraints:constraintsArray];
self.didSetupConstraints = YES;
}
}
感谢。
编辑:
(lldb) po [[UIWindow keyWindow] recursiveDescription]
<UIWindow: 0x7fa91341ce00; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x7fa91341b290>; layer = <UIWindowLayer: 0x7fa91341c610>>
| <UILayoutContainerView: 0x7fa913703e70; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x7fa91343f400>; layer = <CALayer: 0x7fa913702480>>
| | <UINavigationTransitionView: 0x7fa913621e80; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x7fa91361d7e0>>
| | | <UIViewControllerWrapperView: 0x7fa9159678d0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fa91343f150>>
| | | | <UIView: 0x7fa913441190; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fa913443820>>
| | | | | <UITableView: 0x7fa913841400; frame = (0 0; 375 618); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fa91342f520>; layer = <CALayer: 0x7fa913435060>; contentOffset: {0, 0}; contentSize: {375, 15582}>
| | | | | | <UITableViewWrapperView: 0x7fa913445200; frame = (0 0; 375 618); gestureRecognizers = <NSArray: 0x7fa913440320>; layer = <CALayer: 0x7fa9134319d0>; contentOffset: {0, 0}; contentSize: {375, 618}>
| | | | | | | <ColorCell: 0x7fa913637230; baseClass = UITableViewCell; frame = (0 600; 375 150); autoresize = W; layer = <CALayer: 0x7fa9136375b0>>
| | | | | | | | <UITableViewCellContentView: 0x7fa913637630; frame = (0 0; 375 150); gestureRecognizers = <NSArray: 0x7fa913637960>; layer = <CALayer: 0x7fa913637700>>
| | | | | | | | | <UIView: 0x7fa9136379c0; frame = (0 0; 324 150); layer = <CALayer: 0x7fa913637a90>>
| | | | | | | | | | <UIView: 0x7fa913638730; frame = (0 0; 324 100); layer = <CALayer: 0x7fa913638800>>
| | | | | | | | | | | <UIView: 0x7fa913637ab0; frame = (0 0; 0 0); layer = <CALayer: 0x7fa913637b80>>
| | | | | | | | | | | <UIView: 0x7fa913637ba0; frame = (0 0; 0 0); layer = <CALayer: 0x7fa913637c70>>
| | | | | | | | | | | <UIView: 0x7fa913637c90; frame = (0 0; 0 0); layer = <CALayer: 0x7fa913637d60>>
| | | | | | | | | | | <UIView: 0x7fa913637d80; frame = (0 0; 0 0); layer = <CALayer: 0x7fa913637e50>>
| | | | | | | | | | | <UIView: 0x7fa913637e70; frame = (0 0; 0 0); layer = <CALayer: 0x7fa913637f40>>
| | | | | | | | | | <UIView: 0x7fa913638820; frame = (0 100; 324 50); layer = <CALayer: 0x7fa9136388f0>>
| | | | | | | | | | | <UILabel: 0x7fa913637f60; frame = (8 8; 240 34); userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa913636920>>
| | | | | | | | | | | | <_UILabelContentLayer: 0x7fa9135b5ba0> (layer)
| | | | | | | | | | | <UIImageView: 0x7fa913638290; frame = (256 25; 17 0); userInteractionEnabled = NO; layer = <CALayer: 0x7fa913638390>>
| | | | | | | | | | | <UILabel: 0x7fa9136383b0; frame = (281 -100; 35 0); clipsToBounds = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fa913638510>>
奇点: 如果我删除self.contentView.translatesAutoresizingMaskIntoConstraints = NO
和它的作品完美。但奇怪的问题是提出了限制的警告。
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:0x7fad5b9764e0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7fad5b971a20(44)]>",
"<NSLayoutConstraint:0x7fad5b976fb0 V:|-(0)-[UIView:0x7fad5b971db0] (Names: '|':UITableViewCellContentView:0x7fad5b971a20)>",
"<NSLayoutConstraint:0x7fad5b976db0 V:[UIView:0x7fad5b971db0]-(0)-| (Names: '|':UITableViewCellContentView:0x7fad5b971a20)>",
"<NSLayoutConstraint:0x7fad5b9763c0 V:|-(0)-[UIView:0x7fad5b972b70] (Names: '|':UIView:0x7fad5b971db0)>",
"<NSLayoutConstraint:0x7fad5b977730 V:[UIView:0x7fad5b972b70(100)]>",
"<NSLayoutConstraint:0x7fad5b976410 V:[UIView:0x7fad5b972b70]-(0)-[UIView:0x7fad5b972c60]>",
"<NSLayoutConstraint:0x7fad5b9777b0 V:[UIView:0x7fad5b972c60(50)]>",
"<NSLayoutConstraint:0x7fad5b977800 V:[UIView:0x7fad5b972c60]-(0)-| (Names: '|':UIView:0x7fad5b971db0)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fad5b976410 V:[UIView:0x7fad5b972b70]-(0)-[UIView:0x7fad5b972c60]>
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.
因为这不是编程方式创建你应该不需要self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
。此外,您需要在覆盖的结尾处拨打[super updateConstraints]
,而不是开头。编辑包括错误(来自下面的评论):这里发生的是UIView-Encapsulated-Layout-Height是为自定义单元格创建的iOS 8约束。单元格被创建后,如果约束被修改,您将会得到这个错误。有几种方法来管理它;在这种情况下,我还是要提一下,因为你只需要添加约束一次,请尝试添加不在updateConstraints中的约束,而是在initWithStyle:reuseIdentifier中添加约束。
这个问题更加微妙,而iOS8的一个新问题是:在某些时候,你的tableView认为行高是44,所以它创建了一个约束设置单元格高度为44的值。然后添加这些约束条件,但tableView不重新计算高度,所以你会得到错误。如果像@Rob在上面注意到的那样,那么你将高度设置为44就可以了。它不会解决你的问题是一些约束是告诉tableView高度应该是44,但无论你可以应该设置estimatedRowHeight为150,因为这看起来像一个安全的估计,或者如果他们都肯定会是150刚刚设置rowHeight对此。
我以编程方式添加了所有约束。我删除了'self.contentView.translatesAutoresizingMaskIntoConstraints = NO;'并且完美。但**奇怪的问题是提出了限制的警告。 – 2015-02-23 16:58:05
Autolayout警告通常很奇怪,你可以编辑包含它们的问题。 – 2015-02-23 17:06:59
已编辑和更新。 – 2015-02-23 17:23:46
我终于找出了约束警告的原因。
我删除- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
委托方法和警告消失。
因此,答案是明确的:我不应该做的事约束更新在heightForRowAtIndexPath
。相反,我只是返回rowHeight,然后修复问题。
@Rob谢谢sooooo了。你完全正确。我应该接受你们两个的答案。干杯! – 2015-02-24 00:12:41
您关注的是单元格,但我想知道问题是否是tableview本身的约束。它的约束是什么样子的?在Xcode中使用新的[查看调试器](https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/xcode_6_0.html#//apple_ref/doc/uid/TP40014509-SW7) 6,或运行应用程序,暂停执行,并在'(lldb)'提示符处输入'po [[UIWindow keyWindow] recursiveDescription]'并查看表视图的框架本身是什么。 – Rob 2015-02-23 16:40:35
您也可以查看'po [[UIWindow keyWindow] _autolayoutTrace]'以确保您的约束没有任何警告。 – Rob 2015-02-23 16:48:36
@Rob谢谢你的回复。我只是编辑递归描述。 – 2015-02-23 16:51:32