How do I adjust the size of a UITableView in viewDidLoad?

199 Views Asked by At

I have a view controller with two elements: a UITableView and a UIView that contains a UITextField. I am also using the IQKeyboardManager library so that my UITextField is visible. Here is the code that controls them:

override func viewDidLoad() {
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    tableView.register(UINib(nibName: "CommentCell", bundle: nil), forCellReuseIdentifier: "CommentCell")
    tableView.dataSource = self
    self.view.addSubview(tableView)
    commentTextField.delegate = self
    NotificationCenter.default.addObserver(self,
                                               selector: #selector(handle(keyboardShowNotification:)),
                                               name: UIResponder.keyboardDidShowNotification,
                                               object: nil)
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
    print("Did load: \(tableView.frame.size.height)")
}
    
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    commentTextField.becomeFirstResponder()
    print("Did Appear: \(tableView.frame.size.height)")
}
    
    
    
func textFieldDidBeginEditing(_ textField: UITextField) {
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
    print("Did begin: \(tableView.frame.size.height)")
}
    
func textFieldDidEndEditing(_ textField: UITextField) {
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: 0, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + 20))
    print("Did end: \(tableView.frame.size.height)")
}

My problem is that when the view loads the UITableView's height is too large and I cannot see the top of the table view. When I click out of and then back into the UITableView it works just fine but the first load does not work.

here is a sample of output where the view loads, I click out of the UITextField and then click back into it:

Did load: 465.0
Did begin: 465.0
Did Appear: 465.0
Did end: 465.0
Did begin: 211.0
Did end: 465.0
Did begin: 211.0

It seems to me that given that I am setting the height in viewDidLoad that the first three print statements should instead be 211.0. What am I doing wrong?

Edit

I found what the issue is but am unsure of how to solve it. When the view first loads keyboardHeight is 0.0. It is not until I have closed and reopened the keyboard that it gives me an accurate number.

I am using this answer to get keyboardHeight

1

There are 1 best solutions below

0
David On

My problem was I needed to set the UITableView height in viewDidAppear once I did that my problem was solved:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    commentTextField.becomeFirstResponder()
    let textFieldHeight = commentCompositionView.frame.size.height
    let navBarHeight = navigationController?.navigationBar.frame.size.height ?? 0.0
    tableView.frame = CGRect(x: 0, y: keyboardHeight, width: screenSize.width, height: screenSize.height - (textFieldHeight + navBarHeight + keyboardHeight + 20))
}