Neden textViewDidBeginEditing'den önce onKeyboardDisplayed gözlemci çağrılır?

Dec 23 2020

Uygulamam hızlı. Bir UITextField'ı düzenlediğimde, bazen klavye alanı gizliyor. Bu yüzden bir "activeTextField" (ve onu sıfırlamak için textFieldDidEndEditing'i) ayarlamak için textFieldDidBeginEditing temsilcisini kullanıyorum. Daha sonra viewDidLoad'da, gerekirse ekranı yukarı kaydırabilmek için "activeTextField" değerini test ettiğim bir onKeyboardDisplayed fonksiyonuna bağlı bir gözlemci ekliyorum. Ve iyi çalışıyor :)

Kötü haber, aynısını bir UITextView için "activeTextView" ayarlamak için temsilci textViewDidBeginEditing kullanarak yapmaya çalışmamdır. Ancak UITextField'den farklı olarak, temsilci onKeyboardDisplayed'den sonra çağrılır, böylece klavye hala UITextView'umu gizler.

NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisplayed(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

@objc func onKeyboardDisplayed(notification: Notification) {
    guard let keyboardRect = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
        return
    }
    var visibleRect : CGRect = self.view.frame
    visibleRect.size.height -= keyboardRect.height
    if (activeTextField != nil) {
        // Get y position of active textField bottom.
        let textFieldBottomPosition = activeTextField!.convert(CGPoint.zero, to: nil).y + activeTextField!.frame.height
        if(textFieldBottomPosition > visibleRect.size.height) {
            // swipe up
            view.frame.origin.y = (visibleRect.size.height - textFieldBottomPosition - 6)
        }
    }
    if (activeTextView != nil) {
        // Get y position of active textView bottom.
        let textViewBottomPosition = activeTextView!.convert(CGPoint.zero, to: nil).y + activeTextView!.frame.height
        if(textViewBottomPosition > visibleRect.size.height) {
            // swipe up
            view.frame.origin.y = (visibleRect.size.height - textViewBottomPosition - 6)
        }
    }
}

Bunu düzeltmenin bir yolunu biliyor musun?

Yanıtlar

2 syntiz Dec 23 2020 at 20:36

Sonunda burada bir çözüm buldum: UITextView delege olaylarından önce çağrılan klavye olayları

Klavyeyi değiştirdimWillShowNotification

NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisplayed(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)

keyboardDidShowNotification tarafından

NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardDisplayed(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)

Ve şimdi iyi çalışıyor: onKeyboardDisplayed işlevim temsilci textViewDidBeginEditing'den sonra çağrılıyor

1 LucaSfragara Dec 23 2020 at 18:54

Klavye görünümü ile başa çıkmanın standart yolu şudur:

ViewController'ınızda:

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillShow), name: UIControl.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardWillHide), name: UIControl.keyboardWillHideNotification, object: nil)
 }

 @objc private func handleKeyboardWillShow(notification: NSNotification){
    
    guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else{
        return
    }
    self.view.frame.origin.y -= keyboardSize.height
}

@objc private func handleKeyboardWillHide(notification: NSNotification){
    self.view.frame.origin.y = 0
}

Bu, görünüm çerçevesini klavyenin yüksekliğine göre yukarı ve aşağı hareket ettirir. Sorunuzu doğru anlarsam, bunun size yardımcı olabileceğine inanıyorum