Sie können dies als Follow-up-Frage aus meinem vorherigen Beitrag betrachten.Wie berechnet und verwendet Tastaturhöhe (in Swift) in "TextFieldDidBeginEditing", so dass ich UITextfield genau nach oben verschieben kann?
Ich habe eine Logik programmiert, die es mir erlauben würde, das Textfeld nach oben zu bewegen, wenn es hinter der Tastatur versteckt ist. Ab jetzt denke ich, dass die Tastatur 40% des Gerölls von unten in allen Geräten abdecken wird und somit das Textfeld je nach Größe des Geräts verschieben wird. Dies ist natürlich eine schlechte Annahme und ich muss zuerst versuchen, die Tastaturhöhe zu berechnen und dann die Logik anzuwenden.
Meine Forschung im Stapelüberlauf gezeigt Ich kann die Tastaturhöhe mithilfe von Notification berechnen.
Obwohl ich die Logik für Tastaturhöhe folgende Anweisung von Stack-Überlauf geschrieben habe, weiß ich nicht, wie ich meinen Benachrichtigungscode und textFieldDidBeginEditing zusammenführen, um es auszuführen.
Gibt es jemanden, der mir dabei helfen kann? Zusammenfassend möchte ich die Tastaturhöhe innerhalb der textFieldDidBeginEditing-Funktion verwenden, um sie dynamisch zu machen.
unten ist Stück Codes ich geschrieben habe, so weit:
Tastaturhöhe:
func keyboardHeightNotification(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil
)
}
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
print(keyboardHeight)
}
}
verschieben Textfeld oben Code (es funktioniert gut, aber ich brauche Tastatur heigh einzuarbeiten dynamisch zu machen) :
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.frame.maxY > self.view.frame.height * 0.6
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - self.view.frame.height * 0.6 + 2.0), animated: true)
}
else{
return
}
print(textField.frame.maxY)
print(self.view.frame.height * 0.6)
print(textField.frame.maxY - self.view.frame.height * 0.6)
}
func textFieldDidEndEditing(_ textField: UITextField)
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
self.view.endEditing(true);
}
aktualisierten Code aber geben Sie mir noch einen Fehler:
class StudentSignUpViewController: UIViewController,UIScrollViewDelegate, UITextFieldDelegate {
@IBOutlet weak var yourEmail: UITextField!
@IBOutlet weak var yourPassword: UITextField!
@IBOutlet weak var joinUsButton: UIButton!
@IBOutlet weak var back2SignInPage: UIButton!
@IBOutlet weak var scrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
//setting portrait
AppUtility.lockOrientation(.portrait)
//hide keyboard when click outside
self.hideKeyboardWhenTappedAround()
//hide keyboard when click on return
self.yourEmail.delegate = self
self.yourPassword.delegate = self
self.scrollView.delegate = self
//boarder line for nameText
//boarder line for yourEmail
yourEmail.frame.size.height = UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.05)
yourEmail.font = UIFont.italicSystemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.04))
bottomBoader(BottomLine: "UrEmailTextBottomLine", length: 1.0, yourTextBox: yourEmail)
yourEmail.applyCustomClearButton(yourTextBox: yourEmail)
//boarder line for yourPassword
yourPassword.frame.size.height = UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.05)
yourPassword.font = UIFont.italicSystemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.04))
bottomBoader(BottomLine: "UrPasswordTextBottomLine", length: 1.0, yourTextBox: yourPassword)
yourPassword.applyCustomClearButton(yourTextBox: yourPassword)
joinUsButton.layer.cornerRadius = joinUsButton.frame.height * 0.15
back2SignInPage.titleLabel?.font = UIFont.systemFont(ofSize: UIScreen.main.fixedCoordinateSpace.bounds.size.width * CGFloat(0.03))
keyboardHeightNotification()
//textFieldDidBeginEditing(yourPassword)
textFieldDidEndEditing(yourPassword)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
keyboardHeightNotification()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AppUtility.lockOrientation(.portrait)
keyboardHeightNotification()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppUtility.lockOrientation(.all)
}
// ********************************** move text up when keyboard present ****************************************
var kbHeight: CGFloat?
func keyboardHeightNotification(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil
)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
kbHeight = keyboardSize.height
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField.frame.maxY > kbHeight!
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: textField.frame.maxY - (kbHeight! + 2.0)), animated: true)
}
else{
return
}
print(textField.frame.maxY)
print(self.view.frame.height * 0.6)
print(textField.frame.maxY - self.view.frame.height * 0.6)
}
}
}
func textFieldDidEndEditing(_ textField: UITextField)
{
self.scrollView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: true)
self.view.endEditing(true);
}
}
Wenn Ihre Tastatur angezeigt wird, wenn Sie Ihre TextField bearbeiten, ruft Ihr Programm TextFieldDidBeginEditing und KeyboardWillShow Methode auf? Wo fügen Sie Ihren Benachrichtigungsbeobachter hinzu? Nur auf dem func keyboardHeightNotification()? Und du weißt nicht, wo du es richtig platzieren sollst? – Arrabidas92
Hallo, ich weiß nicht, wie man Höhe in keyboardnotification in textFieldDidBeginEditing berechnet. Wo und wie sollte ich keyboardNotification platzieren, damit die Höhe variabel ist. –