问题描述
我有一个自动布局(约束)的应用程序,并注意到,有有定向变更后的实现代码如下一个问题。该再现步骤如下(我有一个非常基本的摄制应用程序 - 用的tableview和添加按钮添加一个新的项目)。
- 旋转你的应用程序,以横向模式使的tableview现在的风景线。
- 的项目添加到tableview中见下文
- 旋转回纵向,则tableview中现在将漂浮在水平锅(如同滚动查看器内容的大小是横向),参见[2]
code以添加
- (IBAction为)使用onAdd:(ID)发送{
算上++;
[self.tableView insertRowsAtIndexPaths:@ [NSIndexPath indexPathForRow:计数1切入口:0] withRowAnimation:UITableViewRowAnimationBottom];
}
[2]浮动表视图
如何解决此问题的任何想法?另外,我怎么能告诉如果这是已知的问题或者是我应该向苹果报告?
我觉得这是一个错误;如果从汽车的布局,并设置适当的调整大小面具转换的项目了,一切都运行得很好。你应该提交一个bug,特别是因为你有很好的再现它一个简单的示例项目。
什么你的情况正在发生的事情是,你怀疑 - 滚动视图的内容视图保持在景观宽度,即使观点本身已经调整为纵向,让你突然得到水平拖动能力。
幸运的是有相当简单的解决方法。我尝试没有成功 setNeedsLayout
或 setNeedsUpdateConstraints
的各种组合,但可以实现这两个解决方案之一:
{
[self.tableView beginUpdates]
[self.tableView endUpdates]
}
或者
<$p$p><$c$c>-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
CGSize contentSize = self.tableView.contentSize;
contentSize.width = self.tableView.bounds.size.width;
self.tableView.contentSize = contentSize;
}
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).
- Rotate your application to Landscape mode so the tableview is now landscape.
- Add an item to the tableview see 1 below
- Rotate back to Portrait, the tableview will now float on horizontal pan (as though the scroll viewer content size is landscape), see [2]
1 Code to add
- (IBAction)onAdd:(id)sender {
count ++;
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}
[2] Floating table view
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.
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];
}
Or,
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
CGSize contentSize = self.tableView.contentSize;
contentSize.width = self.tableView.bounds.size.width;
self.tableView.contentSize = contentSize;
}
这篇关于臭虫UITableView的布局方向改变后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!