我正在尝试在表单中显示TableView。这完全可以在iPhone上正常工作。但是,当我出于某种原因在iPad上进行测试时,其视图比Form Sheet大。这是我正在谈论的图像:
如您所见,文本在框架中并非全部可见。全文为“此内容未完全显示”
这里是它的外观(忽略黑暗模式)
在MainController()中,我在ViewDidLoad()中显示了此页面IntroController()
let navController = UINavigationController(rootViewController: IntroController())
present(navController, animated: true, completion: nil)
在IntroController()中,这是代码。
import UIKit
var tableview: UITableView!
class IntroController: UIViewController, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: TableCell.reuseIdentifier()) as! TableCell
cell.label.text = "Amount"
cell.typeLabel.text = "THIS IS NOT SHOWING FULLY"
cell.selectionStyle = .none
cell.accessoryType = .none
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height), style: .grouped)
view.addSubview(tableview)
tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
tableview.delegate = self
tableview.dataSource = self
}
}
有人知道这是为什么发生以及如何解决吗?
谢谢!
不是将表视图作为子视图添加到VC的view
,而是直接将其设置为view
:
view = tableview
或者,直接使用UITableViewController
。我强烈建议您选择这种方法。
class IntroController: UITableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
...
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
}
}
我不确定是什么原因造成的,但是如果我猜出来,可能是因为view.frame
为viewDidLoad
时由于某种原因未将其设置为VC的内容大小的正整数。叫。如果将将表视图作为view
的子视图添加到viewDidLayoutSubviews
的代码,则它也可以正常工作:
override func viewDidLayoutSubviews() {
if view.subviews.count < 1 {
tableview = UITableView(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height), style: .grouped)
tableview.register(UINib(nibName: TableCell.nibName(), bundle: nil), forCellReuseIdentifier: TableCell.reuseIdentifier())
view.addSubview(tableview)
tableview.delegate = self
tableview.dataSource = self
}
}