Move UIView up when the keyboard appears in iOS

后端 未结 18 1823
再見小時候
再見小時候 2020-11-28 20:38

I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How can I make a UITextField

18条回答
  •  Happy的楠姐
    2020-11-28 21:08

    Bind a view to keyboard is also an option (see GIF at the bottom of the answer)

    Swift 4

    Use an extension: (Wasn't fully tested)

    extension UIView{
        func bindToKeyboard(){
            NotificationCenter.default.addObserver(self, selector: #selector(UIView.keyboardWillChange(notification:)), name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
        }
    
        func unbindToKeyboard(){
            NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillChangeFrame, object: nil)
        }
        @objc
        func keyboardWillChange(notification: Notification) {
            let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
            let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
            let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
            let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
            let deltaY = targetFrame.origin.y - curFrame.origin.y
    
            UIView.animateKeyframes(withDuration: duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
                self.frame.origin.y+=deltaY
    
            },completion: nil)
    
        }
    }
    

    Swift 2 + 3

    Use an extension:

    extension UIView{
        func bindToKeyboard(){
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(UIView.keyboardWillChange(_:)), name: UIKeyboardWillChangeFrameNotification, object: nil)
        }
    
    
        func keyboardWillChange(notification: NSNotification) {
    
            let duration = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! Double
            let curve = notification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as! UInt
            let curFrame = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
            let targetFrame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
            let deltaY = targetFrame.origin.y - curFrame.origin.y
    
    
            UIView.animateKeyframesWithDuration(duration, delay: 0.0, options: UIViewKeyframeAnimationOptions(rawValue: curve), animations: {
                    self.frame.origin.y+=deltaY
    
            },completion: nil)
    
        }  
    }
    

    Usage:

    // view did load...
    textField.bindToKeyboard()
    

    ...

    // view unload
    textField.unbindToKeyboard()
    

    result:

    important
    Don't forget to remove the observer when view is unloading

提交回复
热议问题