2016-05-07 24 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 
     }) 
     } 
    } 
}