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

Con*_*oob 18 xcode objective-c uitableview ios autolayout

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

  1. 将应用程序旋转到横向模式,以便现在可以显示表格视图.
  2. 在表格视图中添加项目,请参阅下面的1
  3. 旋转回纵向,桌面视图现在将浮动在水平平移上(就像滚动查看器内容大小是横向的一样),参见[2]

1要添加的代码

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

[2]浮动表视图

在此输入图像描述

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

jrt*_*ton 30

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

您所怀疑的情况是您所怀疑的 - 滚动视图的内容视图保持在横向宽度,即使视图本身已调整为纵向,因此您突然可以进行水平拖动.

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

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

要么,

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