旋转和调整大小后,UITableView和UITableViewHeader发生冲突

时间:2014-02-13 19:13:13

标签: ios objective-c uitableview rotation frame

我有一个带有tableview标题(不是节标题)的tableview,它们都根据方向调整大小。在我的实现中,我在viewWillLayoutSubviews中为两个视图设置框架。

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

我的目标是拥有纵向,桌面视图的屏幕宽度为长标题。旋转到横向时,宽度减小到屏幕宽度的三分之一,右对齐,标头较短。同样,我需要所有这些变化的旋转动画都是流畅和合乎逻辑的。

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

2 个答案:

答案 0 :(得分: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 :(得分:0)

在iOS 8.0+上,您可以拨打电话:

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

如果覆盖该方法,请确保在某个时刻调用super。

 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)
}