UITextView Vertical Alignment in iOS 7.1

1.5k views Asked by At

I used this codes in my app to align my textView vertically center and it was working until iOS 7.1.

I guess contentSize property is changed in iOS 7.1 how can you help me with that?

    [textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:    (NSDictionary *)change context:(void *)context
{
UITextView * tv = object;
CGFloat topCorrect = (tv.bounds.size.height - tv.contentSize.height * tv.zoomScale) / 2.0;
topCorrect = (topCorrect < 0 ? 0 : topCorrect);
tv.contentOffset = (CGPoint) {.x = tv.contentOffset.x, .y = - topCorrect};
}
1

There are 1 answers

2
adamsiton On BEST ANSWER

I had the same problem and what worked for me was using [tv sizeThatFits:tv.bounds.size].height instead of tv.contentSize.height