簡體   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