UITextview text not showing top lines after rotation in iOS7

强颜欢笑 提交于 2019-12-14 01:27:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!