问题
I have an app that contains a UITextview that displays static text. I used a UITextview to get scrolling for the text, which is much longer than can be displayed in a UILabel. For some reason, the text in a UITextview under iOS 7 does not stay scrolled to the top after a rotation. This works as expected when run under iOS 6.
This can be shown by creating a project with a UITextview that is centered on the storyboard with margins around 50. Then add constraints that pin the UITextview to the edges of the main view. Be sure the text field contains enough text to cause scrolling:
Now run the app and the text will be correctly positioned in the UITextview, with the first line show at the top. After rotation, the text will be shifted down:
After rotating back to portrait, the text is still scrolled down a few lines.
All of this works perfectly if you run it in iOS 6. I have tried have done a lot of research and have tried the following possible solutions in viewWillLayoutSubviews to make the text stay in position:
[textView sizeToFit];
[textView layoutIfNeeded];
[textView setContentOffset:CGPointMake(0.0, 0.0)];
[textView scrollRectToVisible:CGRectMake(0.0, 0.0, 1.0, 1.0) animated:NO];
Nothing seems to work. Does anyone know how to keep the text in position?
回答1:
Try this:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self.textView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}
I tested this on iOS 7.1.
回答2:
Call the UITextView's scrollRangeToVisible: message from the delegate view controller's viewDidLayoutSubviews notification method. This will adjust the range after rotation as well as at first impression.
-(void)viewDidLayoutSubviews {
[self scrollToTop];
}
-(void) scrollToTop {
[self.textView setScrollEnabled:NO];
[self.textView scrollRangeToVisible:NSMakeRange(0.0f, 0.0f)];
[self.textView setScrollEnabled:YES];
}
回答3:
i had the same problem. It looks like using UITextView from the storyboard create lots of small annoying bugs (like this). I managed to solve the problem by creating the UITextView programmatically:
-(void) viewDidLoad{
self.textView=[[UITextView alloc] init];
self.textView.translatesAutoresizingMaskIntoConstraints=NO;
[self.view addSubview:self.textView];
NSDictionary* views = @{@"textView": self.textView};
NSArray* vContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views];
NSArray* hContrains=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-50-[textView]-50-|" options:NSLayoutFormatAlignAllLeft metrics:nil views:views];
[self.view addConstraints:vContrains];
[self.view addConstraints:hContrains];
}
-(void)viewWillLayoutSubviews{
[self.textView layoutIfNeeded];
[self.textView sizeToFit];
}
回答4:
have tried this on iOS 8.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
self.textView.contentInset = UIEdgeInsetsMake(0,0,0,0);
}
回答5:
Similar to some of the above:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
self.textView.scrollRectToVisible(CGRect(x:0,y:0,width:1,height:1),animated: false)
self.textView.layoutIfNeeded()
self.textView.sizeToFit()
}
来源:https://stackoverflow.com/questions/21168268/uitextview-text-not-showing-top-lines-after-rotation-in-ios7