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

Answers


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)
    }

It didnt work well but your answer gives me an opinion. this works, but it works when keyboard is completely open, so its not animating :)

    func animateTextField(up: Bool) {

    var movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {

        if (up == true) {

            self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y - self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
        } else {

            self.loginView.frame = CGRectMake(self.loginView.frame.origin.x, self.loginView.frame.origin.y + self.kbHeight, self.loginView.frame.size.width, self.loginView.frame.size.height)
        }
    })
}

func keyboardWillHide(notification: NSNotification) {

    self.animateTextField(false)

}

func keyboardDidShow(notification: NSNotification) {

    if let userInfo = notification.userInfo {

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

when i put a breakpoint on userInfo line, i get this

[UIKeyboardFrameBeginUserInfoKey: NSRect: {{0, 480}, {320, 216}}, UIKeyboardCenterBeginUserInfoKey: NSPoint: {160, 588}, UIKeyboardFrameEndUserInfoKey: NSRect: {{0, 264}, {320, 216}}, UIKeyboardCenterEndUserInfoKey: NSPoint: {160, 372}, UIKeyboardAnimationDurationUserInfoKey: 0.25, UIKeyboardBoundsUserInfoKey: NSRect: {{0, 0}, {320, 216}}, UIKeyboardAnimationCurveUserInfoKey: 7]

How can i start animation in keyboardWillShow function and use keyboard opening animation. I mean keyboard will start to show, and when it reaches the edge of the view, it'll start pushing. I need to do it in keyboardWillShow function because keyboardDidShow works but not push the view simultaneously


Need Your Help

Tizen Wearable background app

android tizen tizen-wearable-sdk tizen-web-app

I am wondering if it is possible to run an tizen wearable app in background. I need to run an app in background. I am using several sensors and I need to save these data continuously.

PDF XFA Form Read-Only with iTextSharp

c# pdf readonly xfa

I have finally, successfully, figured out how to fill a PDF with an XFA Form with my custom data using iTextSharp.