如何将UI按钮移到UIDatePicker

时间:2019-05-17 19:36:21

标签: swift xcode uiview datepicker

我实现了一段成功的代码,以将视图底部的后退按钮和下一个按钮在显示时移动到键盘上方,并在使用通知中心消失时将其重置为默认值。

我想知道如何做,但是当datePicker在底部弹出时?

这是在弹出时将我的两个按钮移到键盘上方屏幕底部的代码:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)

@objc func keyboardWillShow(notification: NSNotification) {
    guard let userInfo = notification.userInfo else {return}
    guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {return}
    let keyboardFrame = keyboardSize.cgRectValue
    let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
        self.backButton_constrant.constant += keyboardFrame.height
        self.nextButton_constrant.constant += keyboardFrame.height
        self.view.layoutIfNeeded()
    })
}

@objc func keyboardWillHide(notification: NSNotification){
    guard let userInfo = notification.userInfo else {return}
    guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {return}
    let keyboardFrame = keyboardSize.cgRectValue
    let animationDurarion = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! TimeInterval
    UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
        self.backButton_constrant.constant -= keyboardFrame.height
        self.nextButton_constrant.constant -= keyboardFrame.height
        self.view.layoutIfNeeded()
    })

现在很明显,该功能仅在键盘弹出时才有效,而在日期选择器弹出时不起作用,如下所示:

Picture of what the app looks like

(绘制的椭圆形是应在其中放置按钮的位置,但是现在它们已被日期选择器覆盖。)

谢谢

1 个答案:

答案 0 :(得分:0)

我已修复此问题,因为日期选择器充当键盘,因此我也可以将上面问题中显示的相同代码用于日期选择器。 “ NotificationCall”用于任何弹出式窗口,供用户输入。