模糊布局 - 我需要为两个并排视图添加哪些附加约束?

时间:2014-08-28 06:12:22

标签: ios ios7 autolayout nslayoutconstraint

我有一个从导航栏向下滑动的视图 - 允许用户更改标题名称。我正在以编程方式使用自动布局。

我的目标:标签占据了所需的水平空间(例如由于本地化而变化),并固定在其超级视图的左侧(带有边距) )。文本字段占用了剩余的空间,无论其内容如何(并且它与标签之间有边距,右边有边距)。

问题UILabelUITextField都有不明确的布局。除非我的约束是明显错误的,否则我需要添加额外的约束......我只是不确定要添加什么。我在水平访问时尝试了setContentCompressionResistancePrioritysetContentHuggingPriority,但还没有开始工作。

我错过了什么?

以下图片没有拥抱压缩优先级,根据以下代码

它应该如何看起来: enter image description here

编辑短名称可能导致此问题: enter image description here

编辑为长名称可能会导致: enter image description here

以下是代码:

_renameView = [UIView new];
_renameView.translatesAutoresizingMaskIntoConstraints = NO;
_renameView.backgroundColor = _styles.navbarLightGreyTrans;
[self.view addSubview:_renameView];

_renameTextField = [UITextField new];
_renameTextField.translatesAutoresizingMaskIntoConstraints = NO;
_renameTextField.delegate = self;
_renameTextField.clipsToBounds = YES;
_renameTextField.backgroundColor = [UIColor whiteColor];
_renameTextField.layer.cornerRadius = 4.0f;
_renameTextField.layer.borderWidth = 0.7f;
_renameTextField.layer.borderColor = [_styles.greyBorder CGColor];
_renameTextField.font = [UIFont systemFontOfSize:16.0f];
[_renameTextField setReturnKeyType:UIReturnKeyDone];
[_renameView addSubview:_renameTextField];

UIView* separator = [UIView new];
separator.translatesAutoresizingMaskIntoConstraints = NO;
separator.backgroundColor = _styles.greyBorder;
[_renameView addSubview:separator];

UILabel* label = [UILabel new];
label.text = @"Name:";
label.font = [UIFont systemFontOfSize:16.0f];
label.translatesAutoresizingMaskIntoConstraints = NO;
[_renameView addSubview:label];

约束:

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|[_renameView]|"
               options:0
               metrics:nil
               views:views];

[self.view addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|[separator]|"
               options:0
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|-10-[label]-[_renameTextField]-10-|"
               options:NSLayoutFormatAlignAllCenterY
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"V:|-7-[_renameTextField(==36)]-7-[separator(==0.72)]|"
               options:0
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

更新

模糊布局的记录:

|   |   |   |   |   *<UIView:0x10c57d710>
|   |   |   |   |   |   *<UITextField:0x10c57d990> - AMBIGUOUS LAYOUT
|   |   |   |   |   |   |   <UIFieldEditor:0x10c60a150>
|   |   |   |   |   |   |   |   <_UIFieldEditorContentView:0x10c60a710>
|   |   |   |   |   |   |   |   |   <UITextSelectionView:0x10c038850>
|   |   |   |   |   |   |   |   |   |   <UIView:0x10c034730>
|   |   |   |   |   |   |   |   <UIImageView:0x10c60f790>
|   |   |   |   |   |   |   |   <UIImageView:0x10c60fad0>
|   |   |   |   |   |   |   <UIView:0x10c582620>
|   |   |   |   |   |   *<UIView:0x10c582700>
|   |   |   |   |   |   *<UILabel:0x10c5827e0> - AMBIGUOUS LAYOUT
|   |   |   |   |   *<_UILayoutGuide:0x10c582b50> - AMBIGUOUS LAYOUT

2 个答案:

答案 0 :(得分:1)

标签和文本字段之间水平轴上的空间分布不明确。尝试为标签设置内容拥抱优先级。我省略了垂直约束。

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-[textField]-|"
                                                              options:0
                                                              metrics:nil
                                                                views:viewsDictionary]];

[label setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

答案 1 :(得分:0)

我通过修复UILabel的宽度删除了模棱两可的布局。

constraints = [NSLayoutConstraint
               constraintsWithVisualFormat:@"H:|-10-[label(==50)]-[_renameTextField]-10-|"
               options:NSLayoutFormatAlignAllCenterY
               metrics:nil
               views:views];

[_renameView addConstraints:constraints];

无论内容有多长或多短,这都适用于UITextField。当然,我不得不对标签的宽度进行硬编码,这正是我试图避免的......

enter image description here