如何创建一个垂直放置两个视图的布局,填充条目超视图和动态高度?

时间:2016-05-02 18:06:34

标签: ios xcode autolayout

我想使用AutoLayout创建带有注释和注释表单的动态视图,根据注释长度动态更改textview的高度。喜欢Instagram: enter image description here

有一个tableView和一个textView。 我试图添加以下约束 enter image description here

但它表示错误:enter image description here

当我指定静态高度时,它会起作用,但当我尝试添加> =< =时,它总是显示错误。

2 个答案:

答案 0 :(得分:1)

避免不平等。将superview的底部设置为最后一个视图的底部(您可能必须提升所有子视图'垂直压缩阻力优先级高于父级的... ...说明1000,而父级是在750)。或者我建议的策略,将静态高度设置为任意值。对其适当的视图控制器引用约束。在视图控制器中以编程方式计算适当的高度(只需将所有子视图高度相加)。最后将约束常数设置为计算值。

答案 1 :(得分:0)

如果要在键盘出现时更改视图高度,请尝试以下操作:

- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];

    [UIView animateWithDuration:duration animations:^{
        self.bottomViewConstraint.constant = keyboardFrameEnd.size.height + 10.0;
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
    }];

}

- (void)keyboardWillHide:(NSNotification *)note {
    NSDictionary *userInfo = note.userInfo;
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil];


    [UIView animateWithDuration:duration animations:^{
        self.bottomViewConstraint.constant =  10.0;
        [self.view setNeedsUpdateConstraints];
        [self.view layoutIfNeeded];
    }];
}
相关问题