viewcontroller 中的 tableview 将宽度扩展到 ipad(或更大的屏幕设备)

Posted

技术标签:

【中文标题】viewcontroller 中的 tableview 将宽度扩展到 ipad(或更大的屏幕设备)【英文标题】:tableview in viewcontroller expand width to ipad (or larger screen device) 【发布时间】:2017-10-06 19:25:15 【问题描述】:

我在视图控制器中有一个表格视图。对于 iphone6 模拟器,它看起来不错,但是当我在 iPad Air 或更大屏幕的设备上查看它时,tableview 的宽度不会扩大。以下是它的外观:

有没有一种动态的方式可以配置tableview来扩大宽度?

【问题讨论】:

您没有为表格视图设置适当的约束。 我在 tableview 上添加了约束到 right: 0, bottom: 0 但它没有工作。 tableview 还具有为 iphone6 模拟器设置的 width 属性。我认为这就是导致即使为 Ipad 显示器设置宽度的原因。 是的,宽度限制绝对是你的问题。 【参考方案1】:

以编程方式设置它,以便在初始调用期间和 viewWillAppear 中加载:

 let screenSize = UIScreen.main.bounds
 let screenWidth = screenSize.width
 let screenHeight = screenSize.height
 tableView.frame.size.width = screenWidth
 tableView.frame.size.height = screenHeight

【讨论】:

以上是关于viewcontroller 中的 tableview 将宽度扩展到 ipad(或更大的屏幕设备)的主要内容,如果未能解决你的问题,请参考以下文章

如何将tableView的约束设置为viewController?

协助区分 URL 和 Tableview 数据

当我将 tableview 添加到 ViewController 时,tableview 未正确添加

如何使用 Swift 动态访问数组中的项目

containerView的自定义动画中的modalPresentationStyle .overCurrentContext返回黑屏

使用 Swift 3 将数据从 Modal Viewcontroller 传递到 rootController(不要 Segue)