当键盘显示时,我的UIButton在向上移动时没有触发按钮动作

时间:2018-02-28 09:37:05

标签: ios swift

override func viewDidLoad() {
    super.viewDidLoad()
    self.submitBtn.translatesAutoresizingMaskIntoConstraints = true

    emailTf.text = emailID

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    // Do any additional setup after loading the view.
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: #selector(PMSignupViewController.dismissKeyboard))
    tap.cancelsTouchesInView = false
    self.view.addGestureRecognizer(tap)
    // Do any additional setup after loading the view.
}

@objc func dismissKeyboard()
{
    view.endEditing(true)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@objc func keyboardWillShow(notification: NSNotification) {
    print("keyboardWillShow")
    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
    self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: true)
}

@objc func keyboardWillHide(notification: NSNotification){
    print("keyboardWillHide")
    let userInfo:NSDictionary = notification.userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
    self.moveButton(submitBtn, moveDistance: Int(-keyboardHeight), up: false)

}
func moveButton(_ button: UIButton, moveDistance: Int, up: Bool) {
    let moveDuration = 0.3
    let movement: CGFloat = CGFloat(up ? moveDistance : -moveDistance)

    UIView.beginAnimations("animateTextField", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(moveDuration)
    self.submitBtn.frame = self.submitBtn.frame.offsetBy(dx: 0, dy: movement)
    UIView.commitAnimations()
}

@IBAction func submitBtnAction(_ sender: Any) {

}



}

所以基本上我有一个视图,顶部有一个文本字段,底部有一个按钮。当我的键盘启动时,按钮会随之向上移动。但是当按钮位于键盘顶部时,按钮不会触发动作方法。但是当键盘被解除并且它回到屏幕的底部时它工作正常。 有什么输入?

2 个答案:

答案 0 :(得分:1)

你最好使用自动布局,因为框架布局会在更改时出现故障,因此,当IBOutlet名称为buttonBotCon时,挂钩按钮的底部约束

 @objc func keyboardWillShow(notification: NSNotification) {
        print("keyboardWillShow")
        let userInfo:NSDictionary = notification.userInfo! as NSDictionary
        let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.cgRectValue
        let keyboardHeight = keyboardRectangle.height
        self.buttonBotCon.constant = -keyboardHeight
        self.view.layoutIfNeeded()
    }

    @objc func keyboardWillHide(notification: NSNotification){
        print("keyboardWillHide")
       self.buttonBotCon.constant = 0
        self.view.layoutIfNeeded()

    }

编辑:请参阅此处的演示,以完成同样的事情buttonUpKeyboard

答案 1 :(得分:1)

最后我想出了导致问题的原因

我只是要评论出来         tap.cancelsTouchesInView = false 在viewdid加载方法中的这一行...并且工作得很好..但我不知道为什么会这样。