我需要在我正在开发的应用程序之一中制作下表结构。我正在使用 UITableView 创建此表,我需要根据屏幕方向更改表列宽度。我已经为列宽设置了限制条件,我将使用 viewDidLoad() 中的屏幕宽度为这些值分配值。
但是,当屏幕方向发生变化时,我无法弄清楚如何重新对齐这些约束。我发现 viewWillTransition() 将在方向改变时被调用 & 我重新计算了其中的约束 & 为 TableView 调用了 setNeedsLayout() 。但是,当屏幕方向更改时,我无法让我的表格 View 重置表格列宽。我是 IOS 平台的新手,非常感谢任何帮助。
HDR_parName/HDR_parValue/HDR_minValue/HDR_maxValue
Para1/value1/minvalue1/maxvalue1
Para2/value2/minvalue2/maxvalue2
最佳答案
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)
}
然后将调用 tableView 数据源和委托(delegate)方法,您可以在其中根据 tableView 框架大小设置元素大小。
关于ios - UITableViewCell 列宽变化基于屏幕方向 IOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45007890/