繁体   English   中英

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

[英]Resizing tableView with Rotation of Device

我正在尝试创建一个充满我的屏幕的tableView。 并且在启动时,无论桌子是水平的还是垂直的,它总是看起来不错:

普通表视图

但是一旦旋转设备,tableView就会显示其背后的屏幕:

旋转的异常表视图

我首先尝试使用SO的以下代码段推荐:

tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | 
                             UIViewAutoresizingFlexibleHeight

但是我得到了错误:使用未解决的标识符:UIViewAutoresizingFlexibleHeight使用未解决的标识符:UIViewAutoresizingFlexibleHeight

所以我尝试通过以下方式重新加载旋转的框架:

didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation)

但这似乎也不起作用。 请注意,该视图无论横向还是纵向都可以很好地加载,但是一旦视图翻转,它就会变得不正确。

这是我当前在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()

这是我的故事板的图片供参考:

在此处输入图片说明

谢谢您的帮助。

你只需要

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) 
])

和评论

tableView.frame = self.view.bounds

暂无
暂无

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

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