2016-05-07 86 views
0

我正在使用iOS應用程序,當前所有元素都處於滾動視圖中,當鍵盤存在時,我將視圖向上移動250點。這解決了我的問題,但每個設備的鍵盤總是不同的大小。當鍵盤出現時,Swift向上滾動查看

我怎麼能檢測到我的文本字段的屏幕底部有多遠,鍵盤有多高?

回答

2

您應該觀察顯示和隱藏鍵盤的通知。之後,您可以獲得確切的鍵盤大小,並且可以移動或更改滾動視圖的內容插頁。下面是一個示例代碼:

extension UIViewController { 
    func registerForKeyboardDidShowNotification(scrollView: UIScrollView, usingBlock block: (NSNotification -> Void)? = nil) { 
     NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in 
      let userInfo = notification.userInfo! 
      let keyboardSize = userInfo[UIKeyboardFrameBeginUserInfoKey]?.CGRectValue.size 
      let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, keyboardSize!.height, scrollView.contentInset.right) 

      scrollView.scrollEnabled = true 
      scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets) 
      block?(notification) 
     }) 
    } 

    func registerForKeyboardWillHideNotification(scrollView: UIScrollView, usingBlock block: (NSNotification -> Void)? = nil) { 
     NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillHideNotification, object: nil, queue: nil, usingBlock: { (notification) -> Void in 
      let contentInsets = UIEdgeInsetsMake(scrollView.contentInset.top, scrollView.contentInset.left, 0, scrollView.contentInset.right) 
      scrollView.setContentInsetAndScrollIndicatorInsets(contentInsets) 
      scrollView.scrollEnabled = false 
      block?(notification) 
     }) 
    } 
} 

extension UIScrollView { 
    func setContentInsetAndScrollIndicatorInsets(edgeInsets: UIEdgeInsets) { 
     self.contentInset = edgeInsets 
     self.scrollIndicatorInsets = edgeInsets 
    } 
} 

而且在你要在其中轉移了滾動,只需添加viewDidLoad()功能

override func viewDidLoad() { 
    super.viewDidLoad() 

    registerForKeyboardDidShowNotification(scrollView) 
    registerForKeyboardWillHideNotification(scrollView) 
} 
+0

不錯!這適用於UITextField,但不適用於UITextView。任何想法爲什麼? – Vitaly

0

@noir_eagle答案似乎有權根據下一行UIViewController

但可能有一個更簡單的解決方案。也許你可以嘗試使用IQKeyboardManager。它允許你以簡單和無縫的方式處理這些鍵盤事物。

我認爲你至少應該花幾分鐘時間看它。

1

我目前正在爲此工作並找到解決方案。首先,您需要向視圖控制器添加通知,以確定鍵盤是否打開。爲此,您需要在viewDidLoad()中註冊此通知。

override func viewDidLoad() { 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardDidShow:", name: UIKeyboardWillChangeFrameNotification, object: nil) 
} 

而且還別忘了刪除這個通知,當視圖控制器從視圖中移除時。因此,在viewDidDisappear()上刪除此通知。

override func viewDidDisappear(animated: Bool) { 
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillChangeFrameNotification, object: nil) 
} 

而最後一件事是當鍵盤開啓或關閉時管理滾動視圖。因此,首先需要確定鍵盤高度。然後,爲了獲得相當流暢的動畫,您可以使用鍵盤動畫心情和持續時間來爲滾動視圖設置動畫效果。

func keyboardDidShow(notification: NSNotification) { 
    if let userInfo = notification.userInfo { 
     let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() 
     let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0 
     let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber 
     let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue 
     let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw) 
     if endFrame?.origin.y >= UIScreen.mainScreen().bounds.size.height { 
      //isKeyboardActive = false 
      UIView.animateWithDuration(duration, 
      delay: NSTimeInterval(0), 
      options: animationCurve, 
      animations: { 
      // move scroll view height to 0.0 
      }, 
      completion: { _ in 
     }) 
     } else { 
      //isKeyboardActive = true 
      UIView.animateWithDuration(duration, 
      delay: NSTimeInterval(0), 
      options: animationCurve, 
      animations: { 
      // move scroll view height to endFrame?.size.height ?? 0.0 
      }, 
      completion: { _ in 
     }) 
     } 
    } 
} 
相關問題