2016-08-21 2 views
15

Ich versuche, die Ansicht zu verschieben, wenn die Tastatur über die UITextfield, die auf UIScrollView platziert ist. Ich verwende dafür UIKeyboardWillShowNotification und UIKeyboardWillHideNotification.iOS "Gboard" App, UIKeyboard Benachrichtigungshöhe ist falsch oder ungültig

Es funktioniert perfekt, wenn iOS Keyboard verwendet wird, wo ich die Höhe 297 bekomme.

Mein Client verwendet Gboard keyboard, er beschwerte sich, dass die Ansicht nicht bewegt. Wenn ich getestet habe, bekomme ich die Tastaturhöhe als 44.

Ich habe beide Schlüssel probiert UIKeyboardFrameBeginUserInfoKey und UIKeyboardFrameEndUserInfoKey für das NSNotifiction userInfo Objekt. Beide geben nur 44.

Ich versuchte mit UIKeyboardDidShowNotification und UIKeyboardDidHideNotification auch, immer noch das gleiche Problem.

Konnte mir jemand dabei helfen ..?

+1

einen Blick auf diesen Beitrag vorhanden: https://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the- Tastatur-Abdeckungen-ein-Eingabefeld/32555911 # 32555911 könnte es helfen –

+0

@MrH, Danke für den Link:) ... Ich habe es versucht, aber es funktioniert nicht mit 'UITableview'. Ich habe sowohl "UIScrollView" und "UITableView" – arthankamal

+0

bitte Code enthalten, um Tastaturhöhe in oben genannten Frage – iMHitesh

Antwort

-1

KEINE ANTWORT, aber nur hier b/c es ist zu lang, um in Kommentar einzufügen. hier ist mein Code Tastaturhöhe zu bekommen @iMHitesh:

class KeyboardShiftAnimator { 

    fileprivate let showAnimation: (_ keyboardHeight: CGFloat) -> Void 
    fileprivate let hideAnimation: (Void) -> Void 
    fileprivate weak var ownerController: UIViewController! 
    fileprivate let disableAnimations: Bool 

    deinit { 
     NotificationCenter.default.removeObserver(self) 
    } 

    init(ownerController: UIViewController, disableAnimations: Bool = false, showAnimation: @escaping (_ keyboardHeight: CGFloat) -> Void, hideAnimation: @escaping (Void) -> Void) { 
     self.showAnimation = showAnimation 
     self.hideAnimation = hideAnimation 
     self.ownerController = ownerController 
     self.disableAnimations = disableAnimations 

     NotificationCenter.default.addObserver(
      self, 
      selector: 
#selector(KeyboardShiftAnimator.keyboardWillShow(_:)), 
      name: NSNotification.Name.UIKeyboardWillShow, 
      object: nil 
     ) 
     NotificationCenter.default.addObserver(
      self, 
      selector: 
#selector(KeyboardShiftAnimator.keyboardWillHide(_:)), 
      name: NSNotification.Name.UIKeyboardWillHide, 
      object: nil 
     ) 
    } 

    func makeAppropriateLayout(_ duration: NSNumber) { 
     if self.disableAnimations { 
      UIView.performWithoutAnimation({ 
       self.ownerController.view.setNeedsUpdateConstraints() 
       self.ownerController.view.updateConstraintsIfNeeded() 
       self.ownerController.view.layoutIfNeeded() 
      }) 
     } else { 
      self.ownerController.view.setNeedsUpdateConstraints() 
      self.ownerController.view.updateConstraintsIfNeeded() 
      UIView.animate(withDuration: duration.doubleValue, animations: {() -> Void in 
       self.ownerController.view.layoutIfNeeded() 
      }) 
     } 
    } 

    @objc func keyboardWillShow(_ note: Foundation.Notification) { 
     guard let endFrameValue = (note as NSNotification).userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue, 
      let animationDuration = (note as NSNotification).userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber 
      else { return } 

     let keyboardHeight = endFrameValue.cgRectValue.size.height 

     showAnimation(keyboardHeight) 

     makeAppropriateLayout(animationDuration) 
    } 

    @objc func keyboardWillHide(_ note: Foundation.Notification) { 
     let animationDuration = (note as NSNotification).userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber 
     let duration = animationDuration != nil ? animationDuration!.doubleValue : 0.25 

     hideAnimation() 

     makeAppropriateLayout(NSNumber(value: duration)) 
    } 
} 
Verwandte Themen