Cloy Cloy - 3 years ago 147
iOS Question

UiTextfield does not move up when Keyboard pops up ios 11 (Working on Previous versions)

PROBLEM : not working in ios 11 (However working in ios 8)

The following code is written in Swift 2.0. But my application is too large to migrate the code at one go and to release an update.

Aim :
I want to give a release with xcode 7 but i get a 'developer disk image' when debugging on ios 11.
So how can i fix the bug without migrating the code

func viewDidLoad(){

super.baseScrolllView = scrollView
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardAdjust:"), name:UIKeyboardWillHideNotification, object: nil);
}


Code in Base class

var keyboardIsVisible = false
var baseScrolllView: UIScrollView!
func keyboardAdjust(notification: NSNotification) {

let info = notification.userInfo!
let keyboardHeight:CGFloat = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().size.height
let duration:Double = info[UIKeyboardAnimationDurationUserInfoKey] as! Double

var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)

if notification.name == UIKeyboardWillShowNotification && keyboardIsVisible == false{

keyboardIsVisible = true

UIView.animateWithDuration(duration, animations: { ()

var contentInset:UIEdgeInsets = self.baseScrolllView.contentInset
contentInset.bottom = keyboardFrame.size.height
self.baseScrolllView.contentInset = contentInset



})


}else {
keyboardIsVisible = false

UIView.animateWithDuration(duration, animations: { ()
var contentInset:UIEdgeInsets = UIEdgeInsetsZero
self.baseScrolllView.contentInset = contentInset
})
}

}

Answer Source

This is how I get the height of keyboard

@objc func keyboardWillShow(_ notification: Notification) {
    guard let userInfo = notification.userInfo, 
          let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue, 
          let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as? Double else { return }

    containerViewBottomConstraint.constant = keyboardSize.height
    UIView.animate(withDuration: duration) { 
        self.containerView.layoutIfNeeded()
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download