我有一个基于自动布局(约束)的应用程序,并注意到方向更改后的tableview存在问题。 repro步骤如下(我有一个非常基本的repro应用程序 - 带有tableview和一个添加新项目的添加按钮)。
1要添加的代码
- (IBAction)onAdd:(id)sender {
count ++;
[self.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
}
[2]浮动表视图
有关如何解决此问题的任何想法?另外,我如何判断这是否是已知问题或我应向Apple报告的内容?
答案 0 :(得分:30)
我认为这是一个错误;如果您将项目转换为自动布局并设置适当的大小调整掩码,一切正常。您应该提交一个错误,特别是因为您有一个简单的示例项目可以很好地再现它。
您的情况正如您所怀疑的那样 - 滚动视图的内容视图保持在横向宽度,即使视图本身已调整为纵向,因此您突然可以进行水平拖动。
幸运的是,有一些相当简单的解决方法。我尝试了setNeedsLayout
或setNeedsUpdateConstraints
的各种组合但未成功,但您可以实施以下两种解决方案之一:
-(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;
}
答案 1 :(得分:3)
我发现上面的答案大部分时间都有效,但并非所有时间都有效。最强大的解决方案是子类UITableView并在layoutSubviews中设置contentSize:
- (void)layoutSubviews
{
[super layoutSubviews];
CGSize contentSize = self.contentSize;
contentSize.width = self.bounds.size.width;
self.contentSize = contentSize;
}