Swift UItableView 8.4版本布局正确,10.3版本不正确

Swift UItableView 8.4版本布局正确,10.3版本不正确,swift,uitableview,Swift,Uitableview,我使用以下代码,以编程方式创建UITableView override func viewDidLoad() { self.view.addSubview(self.tableView) let width = UIScreen.main.bounds.width+500 self.tableView.frame = CGRect(x: 0, y: 20, width: width, height: self.view.frame.h

我使用以下代码,以编程方式创建UITableView

   override func viewDidLoad() {
       self.view.addSubview(self.tableView)
       let width = UIScreen.main.bounds.width+500
       self.tableView.frame = CGRect(x: 0, y: 20, width: width, height:
       self.view.frame.height)
   }
8.4版本布局:

10.3版本布局:


是版本错误吗?如何修复此问题?

您指定的宽度为500分。超过屏幕的宽度,因此需要固定

假设希望tableview填充屏幕,那么应该从获得宽度的相同位置获得高度。此代码适用于:

 override func viewDidLoad() {
       self.view.addSubview(self.tableView)
       let width = UIScreen.main.bounds.width
       self.tableView.frame = CGRect(x: 0, y: 20, width: width, height:
       UIScreen.main.bounds.height - 20)
   }