当用户输入生日时,如何将两个按钮从屏幕底部移至日期选择器的顶部?我知道有代码可以使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
谢谢
更新:完整代码:
导入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()
}
}
}
关于ios - 当日期选择器 swift 弹出时,如何停止覆盖ui对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56243207/