[英]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.