xcode 方向更改后 UITableView 布局中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14307037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bug in UITableView layout after orientation change
提问by ConfusedNoob
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).
我有一个基于自动布局(约束)的应用程序,并注意到在方向更改后 tableview 存在问题。重现步骤如下(我有一个非常基本的重现应用程序 - 带有一个表格视图和一个添加新项目的添加按钮)。
- Rotate your application to Landscape mode so the tableview is now landscape.
- Add an item to the tableview see 1below
- Rotate back to Portrait, the tableview will now float on horizontal pan (as though the scroll viewer content size is landscape), see [2]
- 将您的应用程序旋转到横向模式,以便 tableview 现在是横向的。
- 将一个项目添加到 tableview 请参见下面的1
- 旋转回纵向,tableview 现在将浮动在水平平移上(好像滚动查看器的内容大小是横向),请参阅 [2]
1Code 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 报告的问题?
回答by jrturton
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:
幸运的是,有相当简单的解决方法。我尝试了各种组合setNeedsLayout
或setNeedsUpdateConstraints
没有成功,但您可以实施以下两种解决方案之一:
-(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;
}
回答by phatmann
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;
}