UITextView中的扩展使用自动布局文本 [英] UITextView that expands to text using auto layout

查看:605
本文介绍了UITextView中的扩展使用自动布局文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局完全使用自动布局编程的视图。我在与上面和下面的项目视图的中间一个UITextView。一切工作正常,但我希望能够扩大UITextView的文本添加。这应该推动它下面都记录下来,因为它扩大。

I have a view that is laid out completely using auto layout programmatically. I have a UITextView in the middle of the view with items above and below it. Everything works fine, but I want to be able to expand UITextView as text is added. This should push everything below it down as it expands.

我知道如何做到这一点的弹簧和支柱的方式,但有这样做的一个自动排版的方式?我能想到的唯一方式是移除,并在每次需要成长的时间重新添加约束。

I know how to do this the "springs and struts" way, but is there an auto layout way of doing this? The only way I can think of is by removing and re-adding the constraint every time it needs to grow.

推荐答案

含UITextView中的观点将与的setBounds 通过自动版式指定它的大小。所以,这就是我所做的。在上海华最初设置了所有的其他的限制,因为他们应该,并且在最后,我把一只特别的约束为的UITextView的高度,我在一个实例变量保存它。

The view containing UITextView will be assigned its size with setBounds by AutoLayout. So, this is what I did. The superview is initially set up all the other constraints as they should be, and in the end I put one special constraint for UITextView's height, and I saved it in an instance variable.

    _descriptionHeightConstraint = [NSLayoutConstraint constraintWithItem:_descriptionTextView
                                     attribute:NSLayoutAttributeHeight 
                                     relatedBy:NSLayoutRelationEqual 
                                        toItem:nil 
                                     attribute:NSLayoutAttributeNotAnAttribute 
                                    multiplier:0.f 
                                     constant:100];

    [self addConstraint:_descriptionHeightConstraint];

的setBounds 的方法,我再改常量的值。

In the setBounds method, I then changed the value of the constant.

-(void) setBounds:(CGRect)bounds
{
    [super setBounds:bounds];

    _descriptionTextView.frame = bounds;
    CGSize descriptionSize = _descriptionTextView.contentSize;

    [_descriptionHeightConstraint setConstant:descriptionSize.height];

    [self layoutIfNeeded];
}

这篇关于UITextView中的扩展使用自动布局文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆