简体   繁体   中英

UITextField - text is jumping and out of aligning when app comes from background to foreground

I have a textField, the text in which jumps and goes out of aligning when I type in.

I did try following after going through many stack questions related to same issue,

When app goes to background,

textField.resignFirstResponder()
textField.layoutIfNeeded()

And when app comes back to foreground,

textField.becomeFirstResponder()
textField.layoutIfNeeded()

Some people pointed out that textField.becomeFirstResponder() should be called in viewDidAppear method, but none of this seems to be working.

Your view controller's lifecycle won't be called when when you transition between active and inactive state. This is a sudden solution I think of.

In applicationDidEnterBackground

self.window?.endEditing(true)

Then when you are becoming active again, check if your current shown vc is this vc with the text field, then only become first responder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM