ios - 根据内容的 UITextView 高度在 iOS 9 中是错误的

标签 ios objective-c iphone ios9 ios9.1

我通过编程方式将 View 动态添加到具有布局约束的 ScrollView 中,对于 TextView 组件,我想根据 TextView 中设置的文本设置高度约束,所以我创建了扩展 UITextView 的类。在 TextView 类中,我编写了以下代码来添加高度约束。

#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 新版本的苹果文档,有一些与自动布局相关的更改。

感谢任何帮助。

最佳答案

不确定您是否解决了这个问题,但我用头撞墙几个小时,直到我确保为我尝试调整大小的 UITextView 将启用滚动设置为 YES。不确定这是否会解决您的问题,它似乎略有不同。

iOS 8 将启用滚动设置为 NO 时可以很好地调整高度,但如果启用滚动设置为 NO,iOS 9 似乎不会调整高度限制。我只是选中了 Storyboard中所有 TextView 的框,然后调整了所有内容的大小。

关于ios - 根据内容的 UITextView 高度在 iOS 9 中是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32782716/

相关文章:

ios - 检测 iOS 应用进入后台

iphone - 如何构建通用ios静态库

ios - 如何在自定义类型的 UIButton 中设置 ImageView 的颜色?

iOS:如何写入项目中特定目录的文件?

ios - Firebase - 当从常量中设置字典中的键时,updateChildValues 会使应用程序崩溃

iphone - iPhone 上的色彩平衡

iphone - 是什么决定了状态栏中是否存在 iPhone 定位服务图标?

Info.plist 中缺少 iOS 9 “fbauth2”

javascript - 如何从 iOS 中保存为应用程序的页面打开新页面?

iphone - kCFCalendarUnitQuarter 没有为每季度触发通知的通知设置下一个触发日期