2015-09-08 23 views
0

我有一個帶有「安全文本輸入」的窗體,我想在鍵盤打開時向上滾動視圖。UIKeyboardWillShowNotification,iOS8和安全文本輸入

我正在監聽UIKeyboardWillShowNotification,但是當焦點位於密碼字段上時,會再次發送此通知,並且我的視圖再次向上滾動一次。

有沒有辦法避免這個問題?

回答

1

第一個解決方案:使用UITableView

第二種解決方案:當鍵盤顯示時不要盲目滾動。在滾動視圖之前檢查視圖的框架或內容偏移量。使功能更可靠。

1

我建議不要移動UIScrollView tbh的偏移量。最好更改contentInset,如下所示:

func keyboardWillShow(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size.height { 
      scrollView.contentInset = UIEdgeInsetsMake(0.0, 0.0, keyboardHeight, 0.0) 
     } 
    } 
} 

func keyboardWillHide(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     if let keyboardHeight = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue().size.height { 
      scrollView.contentInset = UIEdgeInsetsZero 
     } 
    } 
} 

當鍵盤出現時,您可能仍然想要調整contentInstent高度。當你這樣做時,它不會移動視圖,而只是創建鍵盤所需的空間,並且對用戶來說感覺不那麼刺耳。