Ich habe kürzlich damit begonnen, meine eigene App zu entwickeln, indem ich den Anweisungen im Internet folge. Ich bin nur ein Anfänger (ohne Programmierkenntnisse) und kann daher sehr dumme Fehler machen, die ich nicht verstehen kann.UITextField nach oben bewegen, wenn die Tastatur vorhanden ist
In meiner App habe ich eine Situation, in der sich mein E-Mail-Textfeld hinter der Tastatur versteckt, wenn ich versuche, die E-Mail-Adresse einzugeben. Ich habe etwas recherchiert (über Stack-Überlauf) und ein Stück Code geschrieben, das mein Textfeld nach oben verschieben soll, aber es ist nicht ... Ich glaube, dass die Gesamtstruktur des Codes richtig ist (ich mag hier falsch liegen), aber es könnte sein nur ein kleiner Fehler, der meinen Code unwirksam macht.
Kann mir jemand sagen, was ich hier falsch mache?
unten ist das Stück Code, den ich schrieb: {
import UIKit
import WebKit
import CoreGraphics
import AVFoundation
import QuartzCore
import Foundation
class StudentSignUpViewController: UIViewController,UIScrollViewDelegate, UITextFieldDelegate {
@IBOutlet weak var yourEmail: UITextField!
@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.scrollView.delegate = self
//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)
registerForKeyboardNotifications()
deregisterFromKeyboardNotifications()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
AppUtility.lockOrientation(.portrait)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppUtility.lockOrientation(.all)
}
// *************************************************** moving textfiles when keyborad present ***********************************************************
func registerForKeyboardNotifications(){
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func deregisterFromKeyboardNotifications(){
//Removing notifies on keyboard appearing
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.yourEmail {
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}
@objc func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.isScrollEnabled = false
//self.scrollView.contentInset = UIEdgeInsets.zero
}
func textFieldDidBeginEditing(textField: UITextField){
yourEmail = textField
}
func textFieldDidEndEditing(textField: UITextField){
yourEmail = nil
}
Eigentlich gibt es keine Notwendigkeit, sich jemals abzumelden. Es gibt keine Strafe mehr für die Nichtabmeldung; diese Sorge verschwand vor Jahren. – matt
@matt schön, ich werde die Antwort aktualisieren. – sbarow
Hallo, Ich habe versucht, den Code nach dem Entfernen deregisterFromKeyboardNotifications() aus dem Code, aber ohne Glück zu laufen .. was ich habe, ist von ganz unten stehenden Codes Code getan entfernen: { func deregisterFromKeyboardNotifications() {// Entfernen benachrichtigt auf Tastatur erscheinen NotificationCenter.default.removeObserver (self, name: NSNotification.Name.UIKeyboardWillShow, Objekt: null) NotificationCenter.default.removeObserver (self, name: NSNotification.Name.UIKeyboardWillHide, Objekt: null) } } –