I am dynamically adding views to scrollview with layout constraint. For the text view component, I wanted to set a height limit according to the text set in text form, so I created a class that extends UITextView. Inside the text view class, I wrote the following code to add a height limit.
#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
This code gives the correct height in iOS 8.0, but gives the wrong height in iOS 9.0. I checked the apple doc for the new version of iOS 9.0, there are some changes related to the automatic layout.
Any help is appreciated.
Rohit source
share