iOS - 以编程方式复制约束

时间:2015-11-17 22:58:58

标签: ios autolayout uitextview constraints

我在Interface Builder中创建了这个约束。如果没有它,下面的textview会随着内容的增长而向上扩展,随着内容的增长,textview会向下扩展。

enter image description here

如何以编程方式创建该约束?

以下是我的尝试:

    [self addConstraint:[NSLayoutConstraint
                          constraintWithItem:_textView
                          attribute:NSLayoutAttributeTop
                          relatedBy:NSLayoutRelationEqual
                          toItem:_internalScrollView //this is the parent view
                          attribute:NSLayoutAttributeTop
                          multiplier:1.0f
                          constant:0.0]];

但它对任何事物都没有实质性影响。

我使用的UITextView对象来自这个库https://github.com/MatejBalantic/MBAutoGrowingTextView,但这是这个问题的红色鲱鱼。

1 个答案:

答案 0 :(得分:1)

这是你需要做的。

[_internalScrollView addConstraint:[NSLayoutConstraint
                          constraintWithItem:_textView
                          attribute:NSLayoutAttributeTop
                          relatedBy:NSLayoutRelationEqual
                          toItem:_internalScrollView //this is the parent view
                          attribute:NSLayoutAttributeTop
                          multiplier:1.0f
                          constant:300.0]]; // constant should be 300 as shown by you in screen shot

顺便说一下,上面的屏幕截图显示您正在使用顶部布局指南进行约束,而不是使用textView的父视图,如果是这种情况,那么应根据您的需要在上面的代码中更改布局属性