在 iOS7 上的 observeValueForKeyPath 方法上试试这个:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
UITextView *tv = object;
CGFloat height = [tv bounds].size.height;
CGFloat contentheight;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) {
contentheight = [tv sizeThatFits:CGSizeMake(tv.frame.size.width, FLT_MAX)].height;
NSLog(@"iOS7; %f %f", height, contentheight);
}else{
contentheight = [tv contentSize].height;
NSLog(@"iOS6; %f %f", height, contentheight);
}
CGFloat topCorrect = height - contentheight;
topCorrect = (topCorrect <0.0 ? 0.0 : topCorrect);
tv.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
}
被定义为:
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)