当 swift 中弹出日期选择器选择器时,如何阻止 ui 对象被覆盖

How do you stop ui objects being covered when a Date picker selector pops up in swift

当日期选择器在用户输入生日时出现时,我如何将两个按钮从屏幕底部移到顶部?我知道有代码可以让 UI 项在键盘出现时移动,事实上我的代码中已经有了:

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()
    })

我只是想知道如何做同样的事情,但是这次在弹出日期选择器时移动两个按钮,基本上,是否有日期选择器的观察者?

Here’s what my app looks like to make it a bit more understandable

谢谢

更新:完整代码:

进口UI套件

class birthday_createAccountViewController: UIViewController, UITextFieldDelegate {

    var email = String()
    @IBOutlet weak var birthday_input: UITextField!
    @IBOutlet weak var backButton_constraint: NSLayoutConstraint!
    @IBOutlet weak var nextButton_constraint: NSLayoutConstraint!

    private var datePicker: UIDatePicker?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.birthday_input.delegate = self
        birthday_input.underlined()

        datePicker = UIDatePicker()
        datePicker?.datePickerMode = .date
        birthday_input.inputView = datePicker
        datePicker?.addTarget(self, action: #selector(birthday_createAccountViewController.dateChanged(datePicker:)), for: .valueChanged)


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


        // Do any additional setup after loading the view.
    }

    @objc func dateChanged(datePicker: UIDatePicker){

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd/MM/yyyy"
        birthday_input.text = dateFormatter.string(from: datePicker.date)
    }


    @objc func keyboard(notification: NSNotification){
        let rect = ((notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue)
        let keyboardHeight = rect?.height

        if notification.name.rawValue == "UIKeyboardWillShowNotification" {
            if UIDatePicker.isEditing == true { //change this for your picker
                UIView.animate(withDuration: 1) {
                    self.backButton_constraint.constant += keyboardHeight!
                    self.nextButton_constraint.constant += keyboardHeight!
                    self.view.layoutIfNeeded()
                }
            } else {
                UIView.animate(withDuration: 1) {
                    self.backButton_constraint.constant -= keyboardHeight!
                    self.nextButton_constraint.constant -= keyboardHeight!
                    self.view.layoutIfNeeded()
                }
            }
        } else {
            UIView.animate(withDuration: 1) {
                self.backButton_constraint.constant -= keyboardHeight!
                self.nextButton_constraint.constant -= keyboardHeight!
                self.view.layoutIfNeeded()
            }
        }

    }
}

您需要像在不同地点所做的那样添加观察者

private func setObservers() {
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(sender:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(sender:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(sender:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)       
    }

1.- UIKeyboardWillShow 2.- UIKeyboardWillHide 3.- UIKeyboardWillChangeFrame

因此观察者将 运行 在用户执行操作时作为背景 //这段代码来自我的一个项目,我们以它为例

 // Métodos que se ejecutan cuando aparece o desaparece el teclado
    @objc func keyboardWillChange(sender: Notification) {
        print(sender.name.rawValue)

        // Se almacena el rect del teclado, el origen en xy su ancho y su alto
        let rect = ((sender.userInfo![UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue)
        let keyboardHeight = rect?.height

        if sender.name.rawValue == "UIKeyboardWillShowNotification" {
            if Uipcikerdate.isEditing == true { //change this for your picker 
                print("se está editando tokenfield")
                UIView.animate(withDuration: 1) {
                    self.topAnc.isActive = false
                    self.botAnc.isActive = false
                    self.topAnc = self.interface.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: -(keyboardHeight!))
                    self.botAnc = self.interface.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor, constant: -(keyboardHeight!))
                    self.topAnc.isActive = true
                    self.botAnc.isActive = true
                    self.interface.layoutIfNeeded()
                }
            } else {
                UIView.animate(withDuration: 1) {
                    self.topAnc.isActive = false
                    self.botAnc.isActive = false
                    self.botAnc = self.interface.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
                    self.topAnc = self.interface.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)
                    self.topAnc.isActive = true
                    self.botAnc.isActive = true
                    self.interface.layoutIfNeeded()
                }
            }
        } else {
            UIView.animate(withDuration: 1) {
                self.topAnc.isActive = false
                self.botAnc.isActive = false
                self.botAnc = self.interface.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
                self.topAnc = self.interface.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor)
                self.topAnc.isActive = true
                self.botAnc.isActive = true
                self.interface.layoutIfNeeded()
            }
        }

    }