I will try to be as quick as I can. I have a Main view with a container with a height of 0 and whenever the keyboard is enabled I set the container to be the same height of the keyboard and I have a a button and a email field and I have set constraints to this container, basically pushing the elements up. I have set the keyboard to be enabled when the view is loaded and its working normal with he main view but as soon as I press the button to go to the next view, the keyboard opens but the buttons and the email field stay behind the keyboard because the constraints are not working, but when I press the home button and close the app (not from the background) and re open it, the constraints work normally. This only happens when I embed a navigation controller to the main view, otherwise its perfectly working. Any ideas?
I have the exact same code on both Views. Ps: Sorry for the long post, I had no idea how to explain it.
@IBOutlet weak var emailTF: UITextField!
@IBOutlet weak var bottomHeight: NSLayoutConstraint!`
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil
)
// Show keyboard by default
emailTF.becomeFirstResponder()
}
@objc func keyboardWillShow(_ notification: Notification)
{
if let userInfo = notification.userInfo
{
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
{
bottomHeight.constant = keyboardSize.height
view.setNeedsLayout()
}
}
}
Update: I found part of the problem. When loading the second view I wasn't able to get the keyboard height, I changed the second view code from "viewWillAppear" to "viewDidAppear", making the container the same as the keyboard height, BUT there is another issue. When I load the first view I get the keyboard height of 271 (which is correct), when I move to the second view the keyboard height is 226 for some reason, making the textField to move 45. The same thing happens when I press the back button to go back from the secondView to the first, the keyboard height is 226. When I press the home button and reopen the app it doesn't matter which screen I am I get the keyboard height of 271, which is the correct height. What I am doing wrong?
Update 2: SOLVED!
Because my code was only working without the navigation controller I had a feeling that it was something with the quick animation and transition the navigation controller had and it was preventing the code to be read before loading, so I tried to write this line of code
emailTF.resignFirstResponder()
to my button action and it worked! So basically I had to dismiss the keyboard before loading it up in the next view. I hope I helped some users having the same issue.