如何在使用具有多个文本字段的键盘时防止视图向上移动额外空间

时间:2016-02-10 20:12:01

标签: ios swift keyboard textfield

这是我按下文本字段并弹出键盘时移动视图的代码。但是,由于我在一个视图中有两个文本字段,每当我按下第二个文本字段时,视图再次向上移动键盘高度,因此视图离屏幕很远。当第一个文本字段存在时,如何防止用户按下第二个文本字段。我正在使用touchesbegan函数让用户在按下视图的任何位置时隐藏键盘。

按下第一个文本字段时 enter image description here

按下第二个文本字段时 enter image description here

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated: false)
    self.navigationController?.navigationBarHidden = true;

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, withEvent: event)
}

override func viewWillAppear(animated: Bool) {
    super.viewDidAppear(animated)
    self.navigationController?.navigationBarHidden = true;
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)


}

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

func keyboardWillHide(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
    self.view.frame.origin.y += keyboardSize.height
}



func keyboardWillShow(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
    let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size

    if keyboardSize.height == offset.height {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y -= keyboardSize.height
        })
    } else {
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.frame.origin.y += keyboardSize.height - offset.height
        })
    }
}

2 个答案:

答案 0 :(得分:0)

我会看看这个很棒的库来完美地做到这一点。 https://github.com/michaeltyson/TPKeyboardAvoiding

这应该避免你的任何繁重工作。

答案 1 :(得分:0)

您可以使用rikola建议的TPKeyboardAvoiding视图,但我认为您的keyboardWillShow(_:)keyboardWillHide(_:)方法计算中存在一个更简单的问题解决方案。

func keyboardWillHide(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let animationDuration: Double = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

    UIView.animateWithDuration(animationDuration, animations: { () -> Void in
        self.view.frame.origin.y = 0
    })
}

func keyboardWillShow(sender: NSNotification) {
    let userInfo: [NSObject : AnyObject] = sender.userInfo!
    let endSize: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size
    let animationDuration: Double = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

    UIView.animateWithDuration(animationDuration, animations: { () -> Void in
        self.view.frame.origin.y = endSize.height
    })
}

请注意这里的差异。此代码只是使用UIKeyboardFrameEndUserInfoKey查看键盘最终结束的帧,获取动画占用的持续时间UIKeyboardAnimationDurationUserInfoKey并为您的视图设置动画以匹配它。代码的问题在于您正在执行-=+=操作,这意味着该方法依赖于先前的视图状态。在我发布的代码中,该方法是独立于状态的,并且可以连续多次调用,因为系统感觉并且它仍然可以正常运行。

如果您从UIKeyboardAnimationCurveUserInfoKey抓取userInfo并在动画选项中使用该动画曲线以确保视图与键盘的动画完全匹配,那么此处也可获得完成奖励积分正在表演。话虽如此,我从未发现有必要这样做。