2016-08-21 12 views
15

Próbuję przesunąć widok, gdy klawiatura pojawi się na UITextfield, która jest umieszczona na UIScrollView. Używam do tego UIKeyboardWillShowNotification i UIKeyboardWillHideNotification.Aplikacja iOS "Gboard", wysokość powiadomienia UIKeyboard jest nieprawidłowa lub nieważna

Działa doskonale, gdy używa się iOS Keyboard gdzie uzyskuję wysokość 297.

Mój klient używa Gboard keyboard, skarżył się, że widok się nie porusza. Kiedy testowałem, otrzymuję wysokość klawiatury jako 44.

Próbowałem obu klawiszy UIKeyboardFrameBeginUserInfoKey i UIKeyboardFrameEndUserInfoKey dla obiektu NSNotifiction userInfo. Oba dają tylko 44.

Próbowałem także z UIKeyboardDidShowNotification i UIKeyboardDidHideNotification również, nadal ten sam problem.

Czy ktoś może mi pomóc w tym ..?

+1

Wystarczy popatrzeć na to stanowisko: https://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the- keyboard-covers-an-input-field/32555911 # 32555911 może pomóc –

+0

@MrH, Dzięki za link:) ... próbowałem, ale to nie działa z 'UITableview'. Mam zarówno "UIScrollView" i "UITableView'' – arthankamal

+0

proszę podać kod, aby uzyskać wysokość klawiatury w powyższym pytaniu – iMHitesh

Odpowiedz

-1

NIE ODPOWIEDŹ, ale tylko tutaj b/c jest zbyt długi, aby wkleić komentarz. tutaj jest mój kod, aby uzyskać wysokość klawiatury @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)) 
    } 
} 
Powiązane problemy