Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableViewCell上的自动布局出现问题_Ios_Objective C_Uitableview_Autolayout - Fatal编程技术网

Ios UITableViewCell上的自动布局出现问题

Ios UITableViewCell上的自动布局出现问题,ios,objective-c,uitableview,autolayout,Ios,Objective C,Uitableview,Autolayout,我在一个xcode 5项目中遇到自动布局问题。我使用的是一个平面视图控制器和一个导航控制器。我的上半部分有一个MKMapView,下半部分有一个UITableView。我正在使用故事板,并且已经配置了原型UITableViewCell,但是我正在通过代码添加约束。我仔细检查了原型中的每个控件,没有看到在那里配置的任何约束。当我为UITableViewCell添加约束时,就会出现问题。我在单元格中有以下代码: -(void)updateConstraints { [super update

我在一个
xcode 5
项目中遇到自动布局问题。我使用的是一个平面视图控制器和一个导航控制器。我的上半部分有一个
MKMapView
,下半部分有一个
UITableView
。我正在使用
故事板
,并且已经配置了原型
UITableViewCell
,但是我正在通过代码添加约束。我仔细检查了原型中的每个控件,没有看到在那里配置的任何约束。当我为
UITableViewCell
添加约束时,就会出现问题。我在单元格中有以下代码:

-(void)updateConstraints {
    [super updateConstraints];
    //first remove old constraints
    [self removeConstraints:self.constraints];
    [self.nameLabel removeConstraints:self.nameLabel.constraints];
    [self.addressLabel removeConstraints:self.nameLabel.constraints];
    [self.rentableSquareFeetLabel removeConstraints:self.rentableSquareFeetLabel.constraints];
    [self.lastSaleAmountLabel removeConstraints:self.lastSaleAmountLabel.constraints];
    [self.lastSaleDateLabel removeConstraints:self.lastSaleAmountLabel.constraints];
    [self.thumbnailImageView removeConstraints:self.thumbnailImageView.constraints];

    //then set up constraints
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_thumbnailImageView, _nameLabel, _rentableSquareFeetLabel, _lastSaleAmountLabel, _addressLabel, _lastSaleDateLabel);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_thumbnailImageView(60)]-[_nameLabel(<=200)]-(>=8)-[_rentableSquareFeetLabel]-(>=8)-[_lastSaleAmountLabel]|" options:0 metrics:nil views:viewsDictionary]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_nameLabel]-(-4)-[_addressLabel]" options:NSLayoutFormatAlignAllLeading metrics:nil views:viewsDictionary]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_lastSaleAmountLabel]-(-4)-[_lastSaleDateLabel]" options:NSLayoutFormatAlignAllLeading metrics:nil views:viewsDictionary]];
}
-(void)updateConstraints{
[超级更新约束];
//首先删除旧的约束
[自我移除约束:自我约束];
[self.namelab removeConstraints:self.namelab.constraints];
[self.addressLabel removeConstraints:self.nameLabel.constraints];
[self.rentableSquareFeetLabel removeConstraints:self.rentableSquareFeetLabel.constraints];
[self.lastSaleAmountLabel removeConstraints:self.lastSaleAmountLabel.constraints];
[self.lastsaledatalabel移除约束:self.lastsaleamountlab.constraints];
[self.thumbnailImageView移除约束:self.thumbnailImageView.constraints];
//然后设置约束
NSDictionary*viewsDictionary=NSDictionaryOfVariableBindings(\u thumbnailImageView、\u nameLabel、\u rentableSquareFeetLabel、\u lastSaleAmountLabel、\u addressLabel、\u lastSaleDateLabel);
[self-addConstraints:[NSLayoutConstraintsWithVisualFormat:@“H:[U thumbnailImageView(60)]-[U nameLabel(=8)-[U rentableSquareFeetLabel](>=8)-[U lastSaleAmountLabel]。“选项:0度量:无视图:viewsDictionary];
[self-addConstraints:[NSLayoutConstraint Constraints With VisualFormat:@“V:[\u nameLabel]-(-4)-[\u addressLabel]“选项:NSLayoutFormatAlignAllLeading metrics:nil视图:viewsDictionary];
[self-addConstraints:[NSLayoutConstraint Constraints With VisualFormat:@“V:[\u lastSaleAmountLabel]-(-4)-[\u LastSaleAdateLabel]“选项:NSLayoutFormatAlignAllLeading metrics:无视图:viewsDictionary];
}
我在调试控制台中得到以下信息。异常由第一个addConstraints行触发。如果我继续执行这些操作,那么最终所有内容都会显示为它应该显示的样子,因为看起来xcode正在选择打破正确的约束:

2013-09-25 15:07:14.169 PECProperties[32381:a0b] 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)  (
    "<NSIBPrototypingLayoutConstraint:0x9d56c70 'IB auto generated at build time for view with fixed frame' H:|-(0)-[UIImageView:0x9d558f0](LTR)   (Names: '|':UITableViewCellContentView:0x9d55620 )>",
    "<NSIBPrototypingLayoutConstraint:0x9d56d20 'IB auto generated at build time for view with fixed frame' H:[UIImageView:0x9d558f0(60)]>",
    "<NSIBPrototypingLayoutConstraint:0x9d56d80 'IB auto generated at build time for view with fixed frame' H:|-(78)-[UILabel:0x9d559e0](LTR)   (Names: '|':UITableViewCellContentView:0x9d55620 )>",
    "<NSLayoutConstraint:0x9d53830 H:[UIImageView:0x9d558f0]-(NSSpace(8))-[UILabel:0x9d559e0]>" )

Will attempt to recover by breaking constraint  <NSIBPrototypingLayoutConstraint:0x9d56d80 'IB auto generated at build time for view with fixed frame' H:|-(78)-[UILabel:0x9d559e0](LTR)   (Names: '|':UITableViewCellContentView:0x9d55620 )>

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.
2013-09-25 15:07:14.169 PECProperties[32381:a0b]无法同时满足约束。以下列表中可能至少有一个约束是您不想要的约束。请尝试以下操作:(1)查看每个约束,并尝试找出您不期望的约束;(2)查找添加不需要的约束的代码并修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)(
"",
"",
"",
"" )
将尝试通过打破约束进行恢复
在objc_异常_抛出时中断以在调试器中捕获该异常。中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能有用。
第三个NSIBPrototypingLayoutConstraint显示了视图边缘和标签之间的78个点。这是原型的大致位置(如果我在原型中移动它,我会在调试控制台中看到约束的变化),但这与我自己的“标准”约束冲突图像视图和标签之间的距离


我已尝试在视图控制器的
单元格中为rowatinexpath
设置
translatesAutoResizengmaskintoConstraints=NO
,但这似乎也没有帮助。如何修复布局?

这里要介绍的几件事:

  • 正在运行的
    NSIBPrototypingLayoutConstraint
    约束(以及导致异常的约束)是由Interface Builder自动生成的,以使您的故事板或XIB视图布局不含糊。这样做非常隐蔽,但它会自动添加所需的最小约束,以便完全指定每个含糊视图的位置和大小。这是对Xcode 4的更改,因为在Xcode 4中,您无法在Interface Builder中有不明确的布局。使用Xcode 5和更高版本,您可以,但是如果您的布局在编译时不明确,IB将自动为您生成这些约束

    解决此问题的方法是在Interface Builder中添加所需的最小约束,以便完全指定每个视图的位置和大小,然后选择每个不需要的约束,转到右侧边栏属性检查器,并选中占位符-生成时删除旁边的框

    此复选框不仅会删除您添加的约束,而且最重要的是,它将阻止自动生成的IB约束取代它!(您可以想象,当您在IB中有许多视图,并且希望在代码中管理所有约束时,这是非常乏味的。因此,您可能希望避免将IB完全用于视图层次结构,而在这些层次结构中,您希望以编程方式实现自动布局。)

    占位符约束和卸载的约束之间有什么区别?下面是my()中比较这两种约束的幻灯片:

  • updateConstraints
    中,您不想删除约束并重新添加约束。为什么不呢?本质上,这对性能很糟糕,我已经与苹果工程师确认这不是一个好主意。有关更多详细信息,请参阅。为了防止约束被多次添加,使用一个布尔标志(例如,
    hasSetupConstraints
    ),一旦您第一次设置了约束,就将该标志设置为YES,如果再次调用了
    updateConstraints
    ,则如果没有新的约束,就可以立即返回