Autolayout:添加一个视图打破了我的工作调整大小 - 想法?

时间:2013-07-20 20:43:06

标签: ios autolayout

在我的应用中,我有以下设置:

TextView(self.textView)

工具栏

当键盘变得可见时,我添加了一个约束,用于以所需的像素数向上推动文本视图的底部。

spacer =[NSLayoutConstraint
                                   constraintWithItem:self.textView
                                   attribute:NSLayoutAttributeBottom
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:self.view
                                   attribute:NSLayoutAttributeBottom
                                   multiplier:1.0
                                   constant:-height];
[self.view addConstraint:spacer];

当键盘消失时,我删除约束。

这项工作很精细。然而...

我想添加一个位于文本视图顶部的imageview。这似乎很简单。但现在“关闭键盘”调整大小已经破裂。

这是我用来创建imageview的代码,并将其固定到textview边界。

self.overlay = [[UIImageView alloc] init];

[self.overlay setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.overlay];

[self.view addConstraint: [NSLayoutConstraint
                           constraintWithItem:self.overlay
                           attribute:NSLayoutAttributeBottom
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.textView
                           attribute:NSLayoutAttributeBottom
                           multiplier:1.0
                           constant:0]];
[self.view addConstraint: [NSLayoutConstraint
                           constraintWithItem:self.overlay
                           attribute:NSLayoutAttributeTop
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.textView
                           attribute:NSLayoutAttributeTop
                           multiplier:1.0
                           constant:0]];
[self.view addConstraint: [NSLayoutConstraint
                           constraintWithItem:self.overlay
                           attribute:NSLayoutAttributeLeft
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.textView
                           attribute:NSLayoutAttributeLeft
                           multiplier:1.0
                           constant:0]];
[self.view addConstraint: [NSLayoutConstraint
                           constraintWithItem:self.overlay
                           attribute:NSLayoutAttributeRight
                           relatedBy:NSLayoutRelationEqual
                           toItem:self.textView
                           attribute:NSLayoutAttributeRight
                           multiplier:1.0
                           constant:0]];
[self.view layoutIfNeeded];

以下是它的外观:

键盘显示前的初始

Initial before keyboard showing

键盘显示

Keyboard showing

删除了键盘。布局应该回到初始状态,但我得到了这个

Should be back to initial state, but instead I get this

1 个答案:

答案 0 :(得分:0)

解决方案:将工具栏的内容拥抱优先级设置为1.000,以避免它全部展开。