【问题标题】:UITableView and UITableViewHeader conflicting after rotating and resizing旋转和调整大小后 UITableView 和 UITableViewHeader 冲突
【发布时间】:2014-03-12 20:57:35
【问题描述】:

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

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

我的目标是纵向,tableview 是屏幕宽度和长标题。当旋转到横向时,宽度减小到屏幕宽度的三分之一,右对齐,标题更短。同样,我需要使所有这些变化的旋转动画平滑且合乎逻辑。

我对 iOS 旋转和动画实践相当陌生,所以如果有人有更好的建议,我一定会很感激。

【问题讨论】:

    标签: ios objective-c uitableview rotation frame


    【解决方案1】:

    在您重新分配 tableHeader 之前,表格视图不会尊重新的标题高度。

    类似问题:

    当轮换将或确实完成时,您可以执行以下操作:

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

    其实这里也是可以计算header高度的地方:

    - (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;
    }
    

    【讨论】:

    • 嗨,重新分配标题是我所缺少的。设置好表头的frame后,我用self.tableView.tableHeaderView调用了setTableHeaderView。现在一切看起来都很棒。非常感谢。
    • 第一个代码示例中的方法签名应该是-didRotateFromInterfaceOrientation:而不是-didRotateToInterfaceOrientation:duration:
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多