UITextView中的文本不显示在iOS7旋转后前行 [英] UITextview text not showing top lines after rotation in iOS7

查看:202
本文介绍了UITextView中的文本不显示在iOS7旋转后前行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一个显示静态文本一个UITextView的应用程序。我用一个UITextView获取滚动的文字,这是比在一个UILabel显示更长的时间。出于某种原因,在iOS的下一个7 UITextView中的文本不保持滚动到一个旋转后的顶部。这工作时的iOS 6下按预期运行。

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.

这可以通过创建与集中在周围50利润率故事板再加入该引脚的UITextView的主视图的边缘约束一个UITextView一个项目显示。可以肯定的文本字段包含足够的文本会导致滚动:

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:

现在运行应用程序和文本将被正确定位在UITextView中,第一行显示在顶部。旋转之后,文本将下移:

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.

这一切都完美地工作,如果你在IOS 6中运行它,我已经尝试做了很多的研究,并试图viewWillLayoutSubviews以下可能的解决方案,使中位置的文本住宿:

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?

推荐答案

调用的UITextView的scrollRangeToVisible:消息从委托视图控制器的viewDidLayoutSubviews通知方法。这将首​​先IM pression旋转后,以及调整范围。

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];
}

这篇关于UITextView中的文本不显示在iOS7旋转后前行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆