Search code examples
iosswiftuiviewkeyboardmove

how to make a UIView move up when keyboard is present


i have a log in page and for smaller screens (for example iPhone 4s) keyboard can overlay on login button and password textfield etc. So i placed username textfield, password textfield and login button into a UIView which is connected to the ViewController as loginView, and I want to detect if keyboard is on loginView ,it'll push loginView and not overlay on it, and pushing will stop when keyboard is fully open, so loginView will stand just on keyboard. And when keyboard is closed, it'll pull loginView until it reaches its first position.

I tried this but i' pushes all view

var kbHeight: CGFloat!

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);

func animateTextField(up: Bool) {
    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)
    })

}

func keyboardWillShow(notification: NSNotification) {

    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height
            self.animateTextField(true)
        }
    }

}

func keyboardWillHide(notification: NSNotification) {

    self.animateTextField(false)

}

this is the sample i found in many website that people have same problem with me are using this. I changed this self.view.frame = CGRectOffset(self.view.frame, 0, movement) line with self.loginView.frame = CGRectOffset(self.view.frame, 0, movement) becausei want to push my loginView View which has textfields and buttons inside it, but it still dont work and cover login button, so not push properly. I'd be glad if someone can explain me how to do it or show me a source that explains it, i searched but couldnt find, maybe i missed.

Here is my UI http://i62.tinypic.com/1bssy.png

Thanks in advice


Solution

  • Make following change in your code

    Replace this

        func keyboardWillShow(notification: NSNotification) {
    
            if let userInfo = notification.userInfo {
                if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                kbHeight = keyboardSize.height
                    self.animateTextField(true)
                }
            }
        }
    

    with

        func keyboardWillShow(notification : NSNotification) {
    
            var keyboardInfo : NSDictionary = notification.userInfo!
    
            var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
    
    
            var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
            var animationDuration : NSTimeInterval = durationValue.doubleValue
    
            let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
            let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
    
            let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
    
            UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
    
                self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
    
            }, completion: nil)
        }
    

    And

    This

    func keyboardWillHide(notification: NSNotification) {
    
        self.animateTextField(false)
    
    }
    

    with

        func keyboardWillHide(notification : NSNotification) {
    
            var keyboardInfo : NSDictionary = notification.userInfo!
    
            var kbSize : CGSize = ((keyboardInfo.objectForKey(UIKeyboardFrameEndUserInfoKey)) as! CGRect).size
    
    
            var durationValue : NSNumber = keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
            var animationDuration : NSTimeInterval = durationValue.doubleValue
    
            let rawAnimationCurveValue = (keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).unsignedIntegerValue
            let keyboardAnimationCurve = UIViewAnimationCurve(rawValue: rawAnimationCurveValue)
    
            let options = UIViewAnimationOptions(UInt((keyboardInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).integerValue << 16))
    
            UIView.animateWithDuration(animationDuration, delay: 0, options:options , animations: { () -> Void in
    
                self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + kbSize.height, self.loginView.frame.size.width, self.loginView.frame.size.height)
    
            }, completion: nil)
        }