简体   繁体   English

通过设备旋转调整tableView的大小

[英]Resizing tableView with Rotation of Device

I am trying to make a tableView that fills my screen. 我正在尝试创建一个充满我的屏幕的tableView。 And on launch, whether the table is horizontal or vertical, it always looks good: 并且在启动时,无论桌子是水平的还是垂直的,它总是看起来不错:

普通表视图

But once you rotate the device, the tableView reveals the screen behind it: 但是一旦旋转设备,tableView就会显示其背后的屏幕:

旋转的异常表视图

I first tried using a recommendation from SO of this code snippet: 我首先尝试使用SO的以下代码段推荐:

tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
                             UIViewAutoresizingFlexibleHeight

But I got the errors: use of unresolved identifier: UIViewAutoresizingFlexibleHeight use of unresolved identifier: UIViewAutoresizingFlexibleHeight 但是我得到了错误:使用未解决的标识符:UIViewAutoresizingFlexibleHeight使用未解决的标识符:UIViewAutoresizingFlexibleHeight

So I tried to reload the frame on rotation with: 所以我尝试通过以下方式重新加载旋转的框架:

didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)

But that didn't seem to work either. 但这似乎也不起作用。 Note that the view loads nicely in either landscape or portrait, but it goes awry once the view is flipped. 请注意,该视图无论横向还是纵向都可以很好地加载,但是一旦视图翻转,它就会变得不正确。

Here is the code I am currently rolling with in viewDidLoad: 这是我当前在viewDidLoad中滚动的代码:

  super.viewDidLoad()
    tableView = UITableView(frame: view.bounds, style: .plain)
    tableView.backgroundColor = UIColor.blue

    let cellNib = UINib(nibName: "PostTableViewCell", bundle: nil)
    tableView.register(cellNib, forCellReuseIdentifier: "postCell")
    view.addSubview(tableView)
    tableView.register(LoadingCell.self, forCellReuseIdentifier: "loadingCell")
    var layoutGuide:UILayoutGuide!

    layoutGuide = view.safeAreaLayoutGuide

    tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: layoutGuide.leadingAnchor).isActive = true
    tableView.estimatedRowHeight = UITableViewAutomaticDimension
    tableView.frame = self.view.bounds
    tableView.delegate = self
    tableView.dataSource = self
    tableView.tableFooterView = UIView()
    tableView.reloadData()

Here's a pic of my story board for reference: 这是我的故事板的图片供参考:

在此处输入图片说明

Thanks for the help. 谢谢您的帮助。

You only need 你只需要

tableView.translatesAutoresizingMaskIntoConstraints  = false
NSLayoutConstraint.activate([
  tableView.leadingAnchor.constraint(equalTo: layoutGuide.leadingAnchor),
  tableView.topAnchor.constraint(equalTo: layoutGuide.topAnchor),
  tableView.bottomAnchor.constraint(equalTo: layoutGuide.bottomAnchor),
  tableView.trailingAnchor.constraint(equalTo: layoutGuide.trailingAnchor) 
])

And comment 和评论

tableView.frame = self.view.bounds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM