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.
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.
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.
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
Post a Comment