UITextView根据内容的高度在iOS 9中是错误的 [英] UITextView height according to content is wrong in iOS 9

查看:92
本文介绍了UITextView根据内容的高度在iOS 9中是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过编程以动态方式将视图添加到具有布局约束的scrollview中,对于文本视图组件,我想根据textview中设置的文本设置高度约束,因此我创建了扩展UITextView的类.在文本视图类中,我编写了以下代码来添加高度限制.

I adding views dynamically in to scrollview with layout constraint by programatically, For text view component I wanted to set the height constraint according to text set in textview, so I did created class extending UITextView. Inside text view class I have written following code to adding height constraint.

#import "CETextView.h"

@implementation CETextView

- (void)layoutSubviews
{
    [super layoutSubviews];

    if (!self.heightConstraint)
    {
        self.heightConstraint =  [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:100];
        [self addConstraint:self.heightConstraint];
    }
    CGRect lRect = [self contentSizeRect];

    CGSize descriptionSize = lRect.size;
    self.heightConstraint.constant = descriptionSize.height;

}

- (CGRect)contentSizeRect
{
    NSTextContainer* textContainer = [self textContainer];
    NSLayoutManager* layoutManager = [self layoutManager];
    [layoutManager ensureLayoutForTextContainer: textContainer];
    CGRect lRect = CGRectMake(0, 0,320, 500);
    lRect.size = self.contentSize;
    lRect.size.height = lRect.size.height + 5;
    return lRect;
}


@end

此代码在iOS 8.0中给出了正确的高度,但在iOS 9.0中给出了错误的高度.我检查了苹果文档中是否有iOS 9.0的新版本,其中有一些与自动布局有关的更改.

This code gives correct height in iOS 8.0 but gives wrong height in iOS 9.0. I Checked the apple doc for new release of iOS 9.0, there are some of the changes related auto layout.

感谢您的帮助.

推荐答案

不确定您是否解决了这个问题,但是我在脑海中挣扎了几个小时,直到确保将启用滚动设置为是"为止我试图调整大小的UITextViews.不知道这是否可以解决您的问题,似乎有些不同.

Not sure if you solved this or not, but I beat my head against the wall for a few hours until I made sure that enable scrolling was set to YES for the UITextViews I was trying to resize. Not sure if this will solve your issue, it seems slightly different.

在启用滚动设置为否"的情况下,iOS 8可以调整高度的大小,但是如果启用滚动为否",iOS 9似乎不会调整高度限制的大小.我只是在故事板上的所有文本视图上都选中了该框,然后调整了所有大小.

iOS 8 resized the height just fine with enable scrolling set to NO, but it appears iOS 9 will not resize the height constraint if enable scrolling is NO. I just checked the box in the story board on all of my text views and everything resized after that.

这篇关于UITextView根据内容的高度在iOS 9中是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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