2014-02-13 44 views
3

我有一个tableview标题(不是节标题)的tableview,它们都根据方向调整大小。在我的实现中,我为viewWillLayoutSubviews中的两个视图设置了框架。UITableView和UITableViewHeader在旋转和调整大小后发生冲突

我现在的问题是,虽然两个都在旋转后正确单独调整大小,但tableView的原点不会改变,以考虑到标头的高度变化。这导致它们之间出现空间或覆盖某些单元格的标题。

我的目标是在肖像中,tableview的屏幕宽度与一个长头。当旋转到横向时,宽度会减小到屏幕宽度的三分之一,右对齐,并使用较短的标题。同样,我需要所有这些更改的旋转动画平滑和合乎逻辑。

我对iOS旋转和动画实践相当绿色,所以如果任何人有更好的建议,我一定会很感激他们。

回答

1

表视图将不会尊重新的标题高度,除非您重新指定tableHeader。

类似的问题:

当旋转将或没有完成,你可以做到以下几点:

- (void)didRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    self.tableView.tableHeaderView = self.tableView.tableHeaderView; 
} 

事实上,这里也是在那里你可以计算头部的高度的地方:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    CGRect frame = self.tableView.tableHeaderView.frame; 
    frame.size.height = UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ? 50 : 150; 
    self.tableView.tableHeaderView.frame = frame; 

    self.tableView.tableHeaderView = self.tableView.tableHeaderView; 
} 
+1

嗨,重新分配头是我失踪。在设置标题的框架之后,我用self.tableView.tableHeaderView调用了setTableHeaderView。现在一切都很好。非常感谢。 –

+1

第一个代码示例中的方法签名应该是'-didRotateFromInterfaceOrientation:'而不是'-didRotateToInterfaceOrientation:duration:'。 – Frederik

0

在iOS 8.0或更高版本,你可以打电话来代替:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator; 

如果覆盖的方法,请确保调用超级在某一点。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { 

    coordinator.animate(alongsideTransition: { (UIViewControllerTransitionCoordinatorContext) in 



     self.tableView.tableHeaderView? = YourTableHeaderView() 

    }, completion: { (UIViewControllerTransitionCoordinatorContext) in 



    }) 

    super.viewWillTransition(to: size, with: coordinator) 
}