2017-10-15 3 views
1
@objc func keyboardWasShown(_ notification:NSNotification) { 

    var userinfo = notification.userInfo! 

    let kbSize:NSValue = userinfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue 

    let kbRectSize = kbSize.cgRectValue 

    let edgeInsects = UIEdgeInsetsMake(0, 0,kbRectSize.height + 10, 0) 

    self.scrollView.contentInset = edgeInsects 
    self.scrollView.scrollIndicatorInsets = edgeInsects 



    // active text field 

    var aRect:CGRect = self.view.frame 
    aRect.size.height -= kbRectSize.height 


    if(!aRect.contains(activeField.frame.origin)){ 
     scrollView.isScrollEnabled = true 
    scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     aRect = CGRect.zero 
    } 
} 

스크롤 뷰는 의도 한대로 처음 스크롤 한 다음 응답하지 않습니다. Xcode 8.3까지 문제없이 코드가 제대로 작동했습니다.UIKeyboard Notification.userInfo Xcode 9의 주요 버그

버그인지 아닌지와이를 피하는 방법을 확인하십시오. 미리 감사드립니다.

+0

는 참조 - https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 – user1046037

답변

0
    // ==== solution =====// 
      // ==== USE UIKeyboardFrameEndUserInfoKey === 

    // as UIKeyboardFrameBeginUserInfoKey return height as 0 for some 
     //reason on second call onwards , some weird bug indeed. 


    var userinfo = notification.userInfo! 

         // === bug fix ===== 
    let kbSize:NSValue = userinfo[UIKeyboardFrameEndUserInfoKey] as! NSValue 

    let kbRectSize = kbSize.cgRectValue 

    let edgeInsects = UIEdgeInsetsMake(0, 0,kbRectSize.height + 10, 0) 

    self.scrollView.contentInset = edgeInsects 
    self.scrollView.scrollIndicatorInsets = edgeInsects 


    // active text field 

    var aRect:CGRect = self.view.frame 
    aRect.size.height -= kbRectSize.height 

    if(!aRect.contains(activeField.frame.origin)){ 
     scrollView.isScrollEnabled = true 
     scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     aRect = CGRect.zero 
    } 

솔루션, 엑스 코드 9, 키에 약간의 변화가있을 것 같습니다.

대신 UIKeyboardFrameBeginUserInfoKey의 사용 UIKeyboardFrameEndUserInfoKey, 이후 두 번째 통화에 키보드의 적절한 크기를 반환하지 않습니다 어떤 이유로 UIKeyboardFrameBeginUserInfoKey에 대한

으로 값입니다.

0

나는 또한 동일한 문제를 가지고 있으며 일부 변경 작업을 통해 잘 작동합니다. 시나리오에서 코드 아래 시도하고 볼이 작동하는 경우 :

@objc func keyboardWasShown(_ notification:NSNotification) { 

    var keyboardHeight: CGFloat = 260 

    var userinfo = notification.userInfo! 

    let keyboardSize = (userinfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 

    if keyboardSize!.height > 10.0{ 
     keyboardHeight = keyboardSize!.height 
    } 

    let edgeInsects = UIEdgeInsetsMake(0, 0, keyboardHeight + 10, 0) 

    self.scrollView.contentInset = edgeInsects 
    self.scrollView.scrollIndicatorInsets = edgeInsects 

    // active text field 

    var aRect:CGRect = self.view.frame 
    aRect.size.height -= kbRectSize.height 

    if(!aRect.contains(activeField.frame.origin)){ 
     scrollView.isScrollEnabled = true 
     scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     aRect = CGRect.zero 
    } 
} 

내가 가입 화면 내 프로젝트에 코드 아래 사용하고 완벽하게 잘 작동 :

func keyboardWasShown(notification: NSNotification){ 
    //Need to calculate keyboard exact size due to Apple suggestions 
    var keyboardHeight: CGFloat = 260 

    self.scrollView.isScrollEnabled = true 

    var info = notification.userInfo! 
    let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size 
    if keyboardSize!.height > 10.0{ 
     keyboardHeight = keyboardSize!.height 
    } 

    let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0) 

    self.scrollView.contentInset = contentInsets 
    self.scrollView.scrollIndicatorInsets = contentInsets 

    var aRect : CGRect = self.view.frame 
    aRect.size.height -= keyboardHeight 

    if let activeField = self.activeField { 
     if (!aRect.contains(activeField.frame.origin)){ 
      self.scrollView.scrollRectToVisible(activeField.frame, animated: true) 
     } 
    } 
} 

func keyboardWillBeHidden(notification: NSNotification){ 
    //Once keyboard disappears, restore original positions 

    self.scrollView.contentInset = UIEdgeInsets.zero 
    self.scrollView.scrollIndicatorInsets = UIEdgeInsets.zero 
    self.scrollView.isScrollEnabled = false 

} 

그냥 시도하고 디버그와 귀하의 요구 사항에 따라 더 많은 변화가 있습니다. 희망이 작동합니다. 키보드 알림 사용자 정보 사전의 값 쌍 - 찾을 수

+0

나는 그것을 시도 이미 도움이 안된다. – CodeDev