Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust your scrollview for the keyboard in swift 3+

How do you adjust your scrollview to compensate for a keyboard vertically? Read on...

Yes I know this is some basic info, but I randomly noticed today that all of the answers I saw about this topic are all over the place with info, versions and/or use bangs all over the place... but nothing solid for Swift 3+.

like image 549
crewshin Avatar asked May 24 '17 19:05

crewshin


People also ask

How do I move the view up on keyboard in IOS?

The best way to do this is to place your content inside a UIScrollView, then adjust the scroll view's contentInset property by the height of the keyboard when it's shown. Absolutely do not assume the keyboard height--use the value from the "keyboard will show" notification.

How do I get the height of a swift key?

Swift. You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification.


2 Answers

Swift 4.2:

Substitute scrollView for UITableView, UICollectionView, etc.

let scrollView = UIScrollView()

Add observers.

override open func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

Add some functions to listen for the notifications:

@objc func keyboardWillHide(notification: Notification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

@objc func keyboardWillShow(notification: Notification) {
    guard let keyboardFrame: CGRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
    scrollView.contentInset.bottom = keyboardFrame.height
}

Worth noting is that if your deployment target is iOS 9 or greater, you don't need to remove the observer anymore. Check the NotificationCenter docs for more info.

deinit {
    NotificationCenter.default.removeObserver(self)
}

------------------------------------------------

Swift 3:

let scrollView = UIScrollView()

Add observers.

override open func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    NSNotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(noti:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}

Add some functions to listen for the notifications:

func keyboardWillHide(noti: Notification) {
    let contentInsets = UIEdgeInsets.zero
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}

func keyboardWillShow(noti: Notification) {

    guard let userInfo = noti.userInfo else { return }
    guard var keyboardFrame: CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
    keyboardFrame = self.view.convert(keyboardFrame, from: nil)

    var contentInset:UIEdgeInsets = scrollView.contentInset
    contentInset.bottom = keyboardFrame.size.height
    scrollView.contentInset = contentInset
}

Worth noting is that if your deployment target is iOS 9 or greater, you don't need to remove the observer anymore. Check the NotificationCenter docs for more info.

deinit {
    NotificationCenter.default.removeObserver(self)
}
like image 183
crewshin Avatar answered Nov 11 '22 19:11

crewshin


A modification to make it work on iOS 11 is to use UIKeyboardFrameEndUserInfoKey rather than UIKeyboardFrameBeginUserInfoKey. Just a simplified approach to @crewshin's solution:

@objc func keyboardWillShow(_ notification: NSNotification) {                
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        scrollView.contentInset.bottom = keyboardSize.height
    }
}

@objc func keyboardWillHide(_ notification: NSNotification) {        
    scrollView.contentInset.bottom = 0
}
like image 27
Lawliet Avatar answered Nov 11 '22 19:11

Lawliet