当键盘存在时,Swift移动滚动视图

时间:2016-05-07 16:10:12

标签: ios swift keyboard

我正在开发iOS应用程序,目前我的所有元素都在滚动视图中,当键盘存在时,我将视图向上移动250点。这解决了我的问题,但键盘每个设备的大小总是不同。

如何检测屏幕底部与文本字段的距离以及键盘的高度?

3 个答案:

答案 0 :(得分: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
    }
}

在您想要移动滚动视图的UIViewController中,只需在viewDidLoad()函数下添加下一行

override func viewDidLoad() {
    super.viewDidLoad()

    registerForKeyboardDidShowNotification(scrollView)
    registerForKeyboardWillHideNotification(scrollView)
}

答案 1 :(得分: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
        })
        }
    }
}

答案 2 :(得分:0)

@noir_eagle的回答似乎是正确的。

但可能有一个更简单的解决方案。也许你可以尝试使用IQKeyboardManager。它允许您以简单无缝的方式处理这些键盘事物。

我认为你至少应该花几分钟时间来看待它。