Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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 8自动布局、VFL和边距等于或大于_Ios_Autolayout_Constraints_Ios8 - Fatal编程技术网

iOS 8自动布局、VFL和边距等于或大于

iOS 8自动布局、VFL和边距等于或大于,ios,autolayout,constraints,ios8,Ios,Autolayout,Constraints,Ios8,在iOS 8上,我遇到了VFL中的约束问题,而在iOS 6和iOS 7上,一切正常。这就是约束条件: H:|-margin-[_imageView]-(=>margin)-[_label]-margin-| \u imageView\u和\u label都获得了正确的固有宽度,边距按预期增长。我想实现 |-[_imageView]-------------------------------[some text]-| |-[_imageView]--------------------

在iOS 8上,我遇到了VFL中的约束问题,而在iOS 6和iOS 7上,一切正常。这就是约束条件:

H:|-margin-[_imageView]-(=>margin)-[_label]-margin-|
\u imageView\u
\u label
都获得了正确的固有宽度,边距按预期增长。我想实现

|-[_imageView]-------------------------------[some text]-|

|-[_imageView]---------------------------[a larger text]-|

|-[_imageView]-----------------------[a very large text]-|

|-[_imageView]-[a very very very very very very larg...]-|
这在视觉上是正常的,但会引发断开的约束异常:

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7b856ee0 H:[UIImageView:0x7b8ef1f0]-(>=12)-[UILabel:0x7b8e7c60'Test']>
问题可以通过以下步骤解决:

更改约束删除
=
并添加优先级:

H:|-margin-[_imageView]-(margin@750)-[_label]-margin-|
设置
\u imageView

[_imageView setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
设置
\u标签的抗压强度

[_label setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
有了这些规则,在任何平台上都没有问题。所有这些在iOS 8上都是必要的吗?是虫子还是我做错了


谢谢。

我已经从头开始了这个项目,下面是我的代码(实际上很好用):

我认为您的主要问题是没有关闭生成
UIView封装布局的
translatesAutoresizingMaskIntoConstraints
(在iOs8之前我从未见过)。我还没有找到一个地方有很好的文档记录,但是有很多关于这个约束的问题

我还创建了github repo,因此您可以自己尝试:

此外,您还可以查看WWDC2014视频-表格和集合视图中的新增内容(~20分钟)。这里有一些信息,您现在可以看到
ui查看封装布局问题,但稍后会解决。此外,您还可以尝试使用
行高
,因为故事板(或xib)中的所有ios8表视图都应该显式设置

self.tableView.rowHeight = UITableViewAutomaticDimension

我不确定,在这种特殊情况下它是否有用,但也尝试一下吧

我的猜测是,被破坏的约束是由于文本字符串太长而无法满足“>=12”;i、 e.边距+图像+文本的宽度超过父视图的宽度。ios8确实引入了布局边距,但我不太清楚这些边距是如何受到VFL语句的影响的。您能否将标签的行高设置为0,以便它可以垂直展开,并且仍然保持水平约束?@n因为无论文本的长度如何,约束总是被破坏的。我无法更改标签的行数,文本必须被截断。当它抱怨约束被破坏时,你能发布完整的日志和堆栈跟踪吗?如果不设置标签的内容包含优先级和压缩阻力,会发生什么?您是否尝试在UILabel上使用preferredMaxLayoutWidth?设置此选项多次帮助我解决自动布局问题。您可以将preferredMaxLayoutWidth设置为视图的宽度imageView的宽度减去您尝试实现的填充。复制此内容时遇到问题。我从头开始设置了一个快速示例,它的行为似乎符合我的要求,使用了以下内容:
[self.view addConstraints:[NSLayoutConstraint constraints with visualformat:@“H:|-margin-[imageView]-(>=margin)-[label]-margin-[124;”选项:0度量:@{“margin”:@10}视图:绑定]
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
topView.backgroundColor = [UIColor redColor];
topView.translatesAutoresizingMaskIntoConstraints = NO;

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 160)];
imageView.backgroundColor = [UIColor greenColor];
imageView.translatesAutoresizingMaskIntoConstraints = NO;
[topView addSubview:imageView];

self.label = [[UILabel alloc] initWithFrame:CGRectMake(80, 80, 200, 32)];
self.label.backgroundColor = [UIColor yellowColor];
self.label.text = @"some text";
self.label.translatesAutoresizingMaskIntoConstraints = NO;
[topView addSubview:self.label];


self.tableView.tableHeaderView = topView;

NSDictionary *views = @{@"imageView":imageView, @"label":self.label};

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-margin-[imageView(40)]-(>=margin)-[label]-margin-|" options:0 metrics:@{@"margin": @30} views:views];
NSArray *imageConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[imageView(160)]-20-|" options:0 metrics:nil views:views];
NSArray *textConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-40-[label]" options:0 metrics:nil views:views];
NSArray *topConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[topView(320)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(topView)];

[topView addConstraints:constraints];
[topView addConstraints:imageConstraints];
[topView addConstraints:textConstraints];
[topView addConstraints:topConstraints];
self.tableView.rowHeight = UITableViewAutomaticDimension