UITableViewCell 列宽根据屏幕方向 IOS Swift 更改

Aru*_*mar 4 uitableview screen-orientation ios swift

我需要在我正在开发的应用程序之一中制作类似于下表的结构。我正在使用 UITableView 创建这个表格,我需要我的表格列宽根据屏幕方向进行更改。我已经为列宽设置了约束,我将使用viewDidLoad() 中的屏幕宽度为这些值赋值。

但是,当屏幕方向发生变化时,我无法弄清楚如何重新对齐这些约束。我发现viewWillTransition()会在方向改变时被调用 & 我重新计算了里面的约束 & 为表视图调用了 setNeedsLayout() 。但是,当屏幕方向改变时,我无法让我的表格视图重置表格列宽。我是 IOS 平台的新手,任何帮助将不胜感激。

HDR_parName/HDR_parValue/HDR_minValue/HDR_maxValue

Para1/value1/minvalue1/maxvalue1

Para2/value2/minvalue2/maxvalue2

小智 5

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    let animationHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called several times during rotation, 
        // so if you want your tableView change more smooth reload it here too. 
        self?.tableView.reloadData()
    }

    let completionHandler: ((UIViewControllerTransitionCoordinatorContext) -> Void) = { [weak self] (context) in
        // This block will be called when rotation will be completed
        self?.tableView.reloadData()
    }

    coordinator.animateAlongsideTransition(animationHandler, completion: completionHandler)

}
Run Code Online (Sandbox Code Playgroud)

然后将调用 tableView 数据源和委托方法,您可以在其中根据 tableView 框架大小设置元素大小。