【问题标题】:UITextview text not showing top lines after rotation in iOS7在iOS7中旋转后UITextview文本不显示顶行
【发布时间】:2014-02-05 17:49:35
【问题描述】:

我有一个包含显示静态文本的 UITextview 的应用程序。我使用 UITextview 来滚动文本,这比在 UILabel 中显示的要长得多。出于某种原因,iOS 7 下的 UITextview 中的文本在旋转后不会保持滚动到顶部。这在 iOS 6 下运行时按预期工作。

这可以通过创建一个带有 UITextview 的项目来显示,该 UITextview 位于故事板上,边距约为 50。然后添加将 UITextview 固定到主视图边缘的约束。确保文本字段包含足够的文本以引起滚动:

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

旋转回纵向后,文本仍会向下滚动几行。

如果您在 iOS 6 中运行,所有这些都可以完美运行。我已经尝试做了很多研究,并在 viewWillLayoutSubviews 中尝试了以下可能的解决方案,以使文本保持原位:

[textView sizeToFit];
[textView layoutIfNeeded];

[textView setContentOffset:CGPointMake(0.0, 0.0)];

[textView scrollRectToVisible:CGRectMake(0.0, 0.0, 1.0, 1.0) animated:NO];

似乎没有任何效果。有谁知道如何将文本保持在适当位置?

【问题讨论】:

  • 只有我一个人还是这个问题有点安静?抱歉,我怀疑这是一个超级烦人的错误,或者我错过了一些简单的东西。任何见解将不胜感激。

标签: ios7 uitextview autolayout ios7.1


【解决方案1】:

试试这个:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [self.textView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
}

我在 iOS 7.1 上对此进行了测试。

【讨论】:

    【解决方案2】:

    从委托视图控制器的 viewDidLayoutSubviews 通知方法调用 UITextView 的 scrollRangeToVisible: 消息。这将在旋转后以及第一印象时调整范围。

    -(void)viewDidLayoutSubviews {
    
        [self scrollToTop];
    }
    
    -(void) scrollToTop {
        [self.textView setScrollEnabled:NO];
        [self.textView scrollRangeToVisible:NSMakeRange(0.0f, 0.0f)];
        [self.textView setScrollEnabled:YES];
    }
    

    【讨论】:

      【解决方案3】:

      我有同样的问题。看起来使用故事板中的 UITextView 会产生许多烦人的小错误(如this)。我设法通过以编程方式创建 UITextView 来解决问题:

      -(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】:

        已在 iOS 8 上尝试过。

        - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
        {
            self.textView.contentInset = UIEdgeInsetsMake(0,0,0,0);
        }
        

        【讨论】:

          【解决方案5】:

          与上面的一些类似:

          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()
          }
          

          【讨论】:

            猜你喜欢
            • 2011-06-04
            • 1970-01-01
            • 1970-01-01
            • 2013-09-28
            • 2017-09-27
            • 1970-01-01
            • 1970-01-01
            • 2011-01-28
            相关资源
            最近更新 更多