ios - Move view with keyboard using swift 3 -
i have app has text field on lower half of view , have navigation controller bottom of page.
how go moving view upwards while typing can see i'm typing , moving down original place when keyboard disappears? don't want move navigation controller
you have observe changes in keyboard frame , act accordingly show uiview (uitextfield, uitextview etc.) above keyboard.
notificationcenter apple documentation: https://developer.apple.com/documentation/foundation/notificationcenter
other answers @ stackoverflow: how make uitextfield move when keyboard present?
here quick example of view controller:
import uikit class exampleviewcontroller: uiviewcontroller { // mark: - properties @iboutlet weak var exampletextview1: uitextview! @iboutlet weak var exampletextview2: uitextview! @iboutlet weak var exampletextview1bottomconstraint: nslayoutconstraint! @iboutlet weak var exampletextview2bottomconstraint: nslayoutconstraint! var exampletextview1bottomconstraintinitialconstant: cgfloat! var exampletextview2bottomconstraintinitialconstant: cgfloat! var keyboardvisibilityobservers: [nsobjectprotocol] = [] // mark: - view lifecycle override func viewdidload() { super.viewdidload() exampletextview1bottomconstraintinitialconstant = exampletextview1bottomconstraint.constant exampletextview2bottomconstraintinitialconstant = exampletextview2bottomconstraint.constant } override func viewwillappear(_ animated: bool) { super.viewwillappear(animated) // add observers text view 1 keyboardvisibilityobservers.append(createshowkeyboardobserver(keyboardtrigger: exampletextview1, distance: 10.0, constraint: exampletextview1bottomconstraint)) keyboardvisibilityobservers.append(createhidekeyboardobserver(keyboardtrigger: exampletextview1, constraint: exampletextview1bottomconstraint, initialconstraintconstant: exampletextview1bottomconstraintinitialconstant)) // add observers text view 2 keyboardvisibilityobservers.append(createshowkeyboardobserver(keyboardtrigger: exampletextview2, distance: 10.0, constraint: exampletextview2bottomconstraint)) keyboardvisibilityobservers.append(createhidekeyboardobserver(keyboardtrigger: exampletextview2, constraint: exampletextview2bottomconstraint, initialconstraintconstant: exampletextview2bottomconstraintinitialconstant)) } override func viewwilldisappear(_ animated: bool) { super.viewwilldisappear(animated) removeallkeyboardvisibilitynotificationobservers() } // mark: - keyboard event handling private func createshowkeyboardobserver(keyboardtrigger: uiview, distance: cgfloat, constraint: nslayoutconstraint) -> nsobjectprotocol { return notificationcenter.default.addobserver(forname: .uikeyboardwillshow, object: nil, queue: nil) { (notification) in if let userinfo = notification.userinfo, let endframe = (userinfo[uikeyboardframeenduserinfokey] as? nsvalue)?.cgrectvalue { // animation duration , curve user info dictionary let duration: timeinterval = (userinfo[uikeyboardanimationdurationuserinfokey] as? nsnumber)?.doublevalue ?? 0 let animationcurverawnsn = userinfo[uikeyboardanimationcurveuserinfokey] as? nsnumber let animationcurveraw = animationcurverawnsn?.uintvalue ?? uiviewanimationoptions.curveeaseinout.rawvalue let animationcurve: uiviewanimationoptions = uiviewanimationoptions(rawvalue: animationcurveraw) let originyaxisofkeyboardtrigger = keyboardtrigger.convert(keyboardtrigger.bounds.origin, to: self.view).y let preferredoriginyaxisofkeyboardtrigger = endframe.origin.y - distance - keyboardtrigger.bounds.height constraint.constant = constraint.constant - (originyaxisofkeyboardtrigger - preferredoriginyaxisofkeyboardtrigger) // animate changes uiview.animate(withduration: duration, delay: timeinterval(0), options: animationcurve, animations: { self.view.layoutifneeded() }, completion: nil) } } } private func createhidekeyboardobserver(keyboardtrigger: uiview, constraint: nslayoutconstraint, initialconstraintconstant: cgfloat) -> nsobjectprotocol { return notificationcenter.default.addobserver(forname: .uikeyboardwillhide, object: nil, queue: nil) { (notification) in if let userinfo = notification.userinfo { // animation duration , curve user info dictionary let duration: timeinterval = (userinfo[uikeyboardanimationdurationuserinfokey] as? nsnumber)?.doublevalue ?? 0 let animationcurverawnsn = userinfo[uikeyboardanimationcurveuserinfokey] as? nsnumber let animationcurveraw = animationcurverawnsn?.uintvalue ?? uiviewanimationoptions.curveeaseinout.rawvalue let animationcurve: uiviewanimationoptions = uiviewanimationoptions(rawvalue: animationcurveraw) constraint.constant = initialconstraintconstant // animate changes uiview.animate(withduration: duration, delay: timeinterval(0), options: animationcurve, animations: { self.view.layoutifneeded() }, completion: nil) } } } private func removeallkeyboardvisibilitynotificationobservers() { keyboardvisibilityobservers.foreach { (observer) in notificationcenter.default.removeobserver(observer) } keyboardvisibilityobservers.removeall() } }
Comments
Post a Comment