I'm trying to show a TableView inside a Form Sheet. This is totally working as expected on iPhone. However, when I test it on iPad for some reason the view is bigger than the Form Sheet. Here is an Image of what I'm talking about:
As you can see the text isn't all visible in the frame. Full text is "THIS IS NOT SHOWING FULLY"
Here is what it is supposed to look like (ignore the dark mode)
In MainController() I presented this page IntroController() by having this in ViewDidLoad()
let navController = UINavigationController(rootViewController: IntroController())
present(navController, animated: true, completion: nil)
In IntroController() this is the code.
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
}
}
Does anyone know why is this happening and how to fix it?
Thank you!
Instead of adding the table view to the view
of VC as a subview, set it directly as the view
:
view = tableview
Alternatively, use a UITableViewController
directly. I strongly recommend you choose this approach.
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())
}
}
I am not sure what causes this, but if I were to guess, it is probably because that view.frame
is, for some reason, not set to the rect of the content size of VC (yet) when viewDidLoad
is called. If you move the code that adds the table view as a subview of view
to viewDidLayoutSubviews
, then it works as well:
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
}
}