0

在我的帮助下,Auto Layout我以编程方式实现了视图,以处理 iPhone 4、5 和 6 的不同设计布局。该程序在除UITableView. 我在控制台中遇到以下约束问题。

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:0x178be740 V:|-(0)-[UILabel:0x165413b0]   (Names: '|':UITableViewCellContentView:0x16541ac0 )>",
 "<NSLayoutConstraint:0x178b5910 V:[UILabel:0x165413b0(40)]>",
 "<NSLayoutConstraint:0x178a0ac0 V:[UILabel:0x165413b0]-(NSSpace(8))-[UIView:0x1789e5d0]>",
 "<NSLayoutConstraint:0x178a0af0 V:[UIView:0x1789e5d0(1)]>",
 "<NSLayoutConstraint:0x178a0b20 V:[UIView:0x1789e5d0]-(0)-|   (Names: '|':UITableViewCellContentView:0x16541ac0 )>",
 "<NSLayoutConstraint:0x178ab4f0 V:|-(10.5)-[UIImageView:0x1789e4c0]   (Names: '|':UITableViewCellContentView:0x16541ac0 )>",
 "<NSLayoutConstraint:0x178b9ca0 V:[UIImageView:0x1789e4c0(19)]>",
 "<NSLayoutConstraint:0x178b9cd0 V:[UIImageView:0x1789e4c0]-(10.5)-|   (Names: '|':UITableViewCellContentView:0x16541ac0 )>"
)

我尝试了几个帖子,但未能摆脱限制。有人可以对此提出建议吗?

更新代码

//MyCustomeListCell.h
UILabel *listTitle;
UIImageView *listImage;
UIView *listSplitter;

//MyCustomListCell.m
 NSDictionary *viewsDictionary =@{@"listTitle": self.listTitle,@"listImage" : self.listImage,@"listSplitter": self.listSplitter};
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[listTitle(40)]-[listSplitter(1)]|" options:0 metrics:nil views:viewsDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[listSplitter]|" options:0 metrics:nil views:viewsDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-10.5-[listImage(19)]-10.5-|" options:0 metrics:nil views:viewsDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[listTitle]-15-[listImage(13)]-20-|" options:0 metrics:nil views:viewsDictionary]];
4

2 回答 2

1

此格式字符串,"V:|-10.5-[listImage(19)]-10.5-|" 是说您希望 imageView 与其父视图的顶部和底部分开 10.5 点,但高度也要 19 点。因此,除非超级视图在所有屏幕尺寸中都恰好是 40 点高,否则这将导致错误。

与此相同,“V:|[listTitle(40)]-[listSplitter(1)]|” 这个只有在超级视图高 49 点(破折号为 40 + 8(我认为)+ 1)时才有效。

要解决这些问题,您不需要对系统进行如此限制。根据您的需要,您可以删除“|” 从这些格式字符串的一端,或摆脱大小设置。

于 2015-01-23T16:28:21.447 回答
1

有更简单的方法,不使用约束但是autoresizingMask

self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
于 2015-01-24T13:08:01.130 回答