微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS 8 autolayout,VFL和margin等于或大于

我在iOS 8中遇到了VFL的限制问题,而在6和7这里就可以了.这是约束:
H:|-margin-[_imageView]-(=>margin)-[_label]-margin-|

_imageView_and _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']>

印刷_autolayoutTrace之后没有歧义.

但是,如果约束只涉及标签,那根本就没有问题:

H:|-margin-[_label1]-(=>margin)-[_label2]-margin-|

以下步骤可以解决问题:

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

H:|-margin-[_imageView]-(margin@750)-[_label]-margin-|

设置_imageView的拥挤优先级

[_imageView setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];

设置_label的抗压缩性

[_label setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

有了这些规则,任何平台都没有问题.所有这些在iOS 8上都是必要的吗?这是一个错误还是我做错了?

谢谢.

解决方法

我从头开始开始项目,这里是我的代码(实际工作正常):
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(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];

我认为你的主要问题是你不会关闭翻译自动化的MaskIntoConstraints来生成UIView-Encapsulated-Layout(我在iOs8之前从未见过).我没有找到一个记录好的地方,但是有关于这个约束的问题很多.

我也创建了github回购,所以你可以自己尝试:https://github.com/Nikita2k/constraintsTest

此外,您可以看一下WWDC2014视频 – 表和集合视图中的新功能(〜20分钟).有一些信息,您现在可以看到UIView-Encapsulated-Layout问题,但稍后会被修复.此外,您可以尝试使用rowHeight,因为故事板(或xib)中的所有ios8 tableView都应显式设置

self.tableView.rowHeight = UITableViewAutomaticDimension

我不确定,会在这个特殊情况下是否有帮助,但也请试试!

原文地址:https://www.jb51.cc/iOS/329350.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐