简体   繁体   English

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

[英]bug in UITableView layout after orientation change

I have an auto-layout (constraint) based application and noticed that there is a problem with a tableview after an orientation change. 我有一个基于自动布局(约束)的应用程序,并注意到方向更改后的tableview存在问题。 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). repro步骤如下(我有一个非常基本的repro应用程序 - 带有tableview和添加新项目的添加按钮)。

  1. Rotate your application to Landscape mode so the tableview is now landscape. 将应用程序旋转到横向模式,以便现在可以显示表格视图。
  2. Add an item to the tableview see 1 below 在表格视图中添加项目,请参阅下面的1
  3. Rotate back to Portrait, the tableview will now float on horizontal pan (as though the scroll viewer content size is landscape), see [2] 旋转回纵向,桌面视图现在将浮动在水平平移上(就像滚动查看器内容大小是横向的一样),参见[2]

1 Code to add 1要添加的代码

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

[2] Floating table view [2]浮动表视图

在此输入图像描述

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? 另外,我如何判断这是否是已知问题或我应该向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. 您所怀疑的情况是您所怀疑的 - 滚动视图的内容视图保持在横向宽度,即使视图本身已调整为纵向,因此您突然可以进行水平拖动。

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: 我尝试了setNeedsLayoutsetNeedsUpdateConstraints各种组合但没有成功,但您可以实现以下两种解决方案之一:

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

Or, 要么,

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

I have found that the answers above work most of the time but not all of the time. 我发现上面的答案大部分时间都有效,但并非所有时间都有效。 The most robust solution is to subclass UITableView and set the contentSize within layoutSubviews: 最强大的解决方案是子类UITableView并在layoutSubviews中设置contentSize:

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGSize contentSize = self.contentSize;
    contentSize.width  = self.bounds.size.width;
    self.contentSize   = contentSize;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM