swift - Sliding UIView When Keyboard Appears in iOS -


i know there lot of similar questions on here sliding uiview when keyboard appears on ios. however, seem having issue implementation haven't seen mentioned. when slide animation occurs, doesn't start top of screen expect, rather, starts lower origin , moves top of screen.

uiview sliding incorrectly

here's class extension i've created achieve slide-up behavior.

public extension uiviewcontroller {      private dynamic func keyboardwillshow(sender: nsnotification) {         if view.frame.origin.y == 0 {             slideviewverticallyforkeyboardheight(sender, directionup: true)         }     }      private dynamic func keyboardwillhide(sender: nsnotification) {         if view.frame.origin.y < 0 {             slideviewverticallyforkeyboardheight(sender, directionup: false)         }     }      private func slideviewverticallyforkeyboardheight(notification: nsnotification, directionup: bool) {         uiview.beginanimations(nil, context: nil)         let keyboardheight: cgfloat = (notification.userinfo?[uikeyboardframebeginuserinfokey] as? nsvalue)?.cgrectvalue().height ?? 0.0         let animationduration = notification.userinfo?[uikeyboardanimationdurationuserinfokey]?.doublevalue ?? 0.0         let animationcurve: int = notification.userinfo?[uikeyboardanimationcurveuserinfokey]?.integervalue ?? 0         uiview.setanimationduration(animationduration)         uiview.setanimationcurve(uiviewanimationcurve(rawvalue: animationcurve)!)         uiview.setanimationbeginsfromcurrentstate(true)          var newframerect = view.frame         if directionup {             newframerect.origin.y -= keyboardheight             newframerect.size.height += keyboardheight         } else {             newframerect.origin.y += keyboardheight             newframerect.size.height -= keyboardheight         }         view.frame = newframerect         uiview.commitanimations()     }      public func registerviewslideonkeyobard() {         nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillshow:", name: uikeyboardwillshownotification, object: nil)         nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboardwillhide:", name: uikeyboardwillhidenotification, object: nil)     }      public func unregisterviewslideonkeyboard() {         nsnotificationcenter.defaultcenter().removeobserver(self, name: uikeyboardwillshownotification, object: nil)         nsnotificationcenter.defaultcenter().removeobserver(self, name: uikeyboardwillhidenotification, object: nil)     } } 

i'm not sure i'm missing. looking @ view.frame value, have negative origin.y expect. see i'm missing?

edit: user aimak made sensible suggestion change (not deprecated) block-call version of animation. interestingly, animation still broken, in different way. here modified code , screen capture of happens more up-to-date animation call.

enter image description here

private func slideviewverticallyforkeyboardheight(notification: nsnotification, directionup: bool) {     uiview.beginanimations(nil, context: nil)     let keyboardheight: cgfloat = (notification.userinfo?[uikeyboardframebeginuserinfokey] as? nsvalue)?.cgrectvalue().height ?? 0.0     let animationduration = notification.userinfo?[uikeyboardanimationdurationuserinfokey]?.doublevalue ?? 0.0     let animationcurve: uiviewanimationcurve = uiviewanimationcurve(rawvalue: notification.userinfo?[uikeyboardanimationcurveuserinfokey]?.integervalue ?? 0)!     var newframerect = view.frame     if directionup {         newframerect.origin.y -= keyboardheight         newframerect.size.height += keyboardheight     } else {         newframerect.origin.y += keyboardheight         newframerect.size.height -= keyboardheight     }      uiview.animatewithduration(animationduration, delay: 0, options: animationcurve.tooptions(), animations: {             self.view.frame = newframerect         }, completion: nil) } 

this might not help, animation methods discouraged since ios 4+ :

use of method discouraged in ios 4.0 , later. should use block-based animation methods specify animations instead.

reference : https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/#//apple_ref/occ/clm/uiview/commitanimations

have tried block-based api ?

edit : autolayout issue !

ok, think have : use autolayout ? if so, subviews of uiviewcontroller.view have constraints view.top = 0, , constraint never broken. if want achieve animation you'd want, consider : 1. embed every subview inside scrollview (then animate contentoffet.y change) or 2. remove/edit view.top constraint


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -