方向更改后 UITableView 布局中的错误

bug in UITableView layout after orientation change(方向更改后 UITableView 布局中的错误)

本文介绍了方向更改后 UITableView 布局中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于自动布局(约束)的应用程序,并注意到在方向更改后 tableview 存在问题.repro 步骤如下(我有一个非常基本的 repro 应用程序 - 带有一个 tableview 和一个添加新项目的添加按钮).

I have an auto-layout (constraint) based application and noticed that there is a problem with a tableview after an orientation change. The repro steps are as follows (I have a very basic repro app - with a tableview and an add button that adds a new item).

  1. 将您的应用程序旋转到横向模式,以便 tableview 现在是横向的.
  2. 向 tableview 添加项目,请参阅下面的 1
  3. 旋转回纵向,tableview 现在将浮动在水平平移上(好像滚动查看器的内容大小是横向的),请参阅 [2]

1要添加的代码

- (IBAction)onAdd:(id)sender {
    count ++;
    [self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}

[2] 浮动表格视图

关于如何解决这个问题的任何想法?另外,我如何判断这是已知问题还是我应该向 Apple 报告的问题?

Any ideas on how to work around this problem? Also, how can I tell if this is known issue or something I should report to Apple?

推荐答案

我认为这是一个错误;如果您将项目从自动布局转换并设置适当的调整大小蒙版,一切正常.您应该提交一个错误,特别是因为您有一个可以很好地重现它的简单示例项目.

I think this is a bug; if you convert the project away from auto layout and set appropriate resizing masks, everything works just fine. You should file a bug, particularly since you have a simple sample project that reproduces it nicely.

在您的情况下发生的事情正如您所怀疑的那样 - 滚动视图的内容视图保持横向宽度,即使视图本身已调整为纵向,因此您突然能够水平拖动.

What is happening in your case is as you suspected - the scroll view's content view remains at the landscape width, even though the view itself has resized to portrait, so you suddenly get the ability to horizontally drag.

幸运的是,有相当简单的解决方法.我尝试了 setNeedsLayout 或 setNeedsUpdateConstraints 的各种组合但没有成功,但您可以实现以下两种解决方案之一:

Luckily there are fairly simple workarounds. I experimented with various combinations of setNeedsLayout or setNeedsUpdateConstraints without success, but you can implement one of these two solutions:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

或者,

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGSize contentSize = self.tableView.contentSize;
    contentSize.width = self.tableView.bounds.size.width;
    self.tableView.contentSize = contentSize;
}

这篇关于方向更改后 UITableView 布局中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:方向更改后 UITableView 布局中的错误

基础教程推荐