gpt4 book ai didi

ios - 当快速弹出日期选择器选择器时,如何阻止 ui 对象被覆盖

转载 作者:行者123 更新时间:2023-11-29 05:44:54 30 4
gpt4 key购买 nike

当用户输入生日时出现日期选择器时,如何将两个按钮从屏幕底部移动到日期选择器的顶部?我知道有一些代码可以让 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.- UIKeyboardWillShow2.- UIKeyboardWillHide3.- UIKeyboardWillChangeFrame

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

 // 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 - 当快速弹出日期选择器选择器时,如何阻止 ui 对象被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56243207/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com