簡體   English   中英

當一個日期選擇器選擇器在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()
    })

我只是想知道我是如何做同樣的事情,但這次在日期選擇器彈出時移動兩個按鈕,基本上,是否有日期選擇器的觀察者?

這是我的應用程序的樣子,使其更容易理解

謝謝

更新:完整代碼:

導入UIKit

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

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM