How would I move two buttons from the bottom of the screen to the top of a datepicker when it appears when the user goes to enter their birthday? I know there's code to make UI items move when a keyboard shows up, in fact that's already in my code:
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()
})
I'm just wondering how I'd do the same, but this time moving the two buttons when the date picker pops up, basically, is there an observer for date pickers?
Here's what my app looks like to make it a bit more understandable
Thanks
UPDATE: FULL CODE:
import 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()
}
}
}
}
you need to add Observers like you did for different ocations
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
So the observers will run background while the user performs actions //this piece of code is from one of my projects us it as example
// 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()
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.