2017-10-21 28 views
0

Il y a plusieurs TextFields dans un UIView.I veulent se déplacer vue lorsque l'utilisateur modifier TextField.And puis-je essayer de référence Move view with keyboard using Swiftvue Déplacer les questions de clavier. (Taille du clavier && vue déplacer ancien numéro)

Mon code de base est ci-dessous:

//MARK: Properties 
@IBOutlet weak var userAccountTextField: UITextField! 
@IBOutlet weak var passwordTextField: UITextField! 
@IBOutlet weak var confirmPasswordTextField: UITextField! 
@IBOutlet weak var emailTextField: UITextField! 
@IBOutlet weak var phoneTextField: UITextField! 

//MARK: Callback 
override func viewDidLoad() { 
    super.viewDidLoad() 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: self.view.window) 
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: self.view.window) 
} 

func keyboardWillShow(notification: NSNotification) { 
     if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size { 
      if self.view.frame.origin.y == 0{ 
       //self.view.frame.origin.y -= keyboardSize.height 
       self.view.frame.origin.y -= 150 
      } 
     } 
    } 

func keyboardWillHide(notification: NSNotification) { 
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size { 
     if self.view.frame.origin.y != 0{ 
      //self.view.frame.origin.y += keyboardSize.height 
      self.view.frame.origin.y += 150 
     } 
    } 

} 

Il y a deux questions:

1. keyboardSize.height avec masquer le clavier est plus grand que keyboardSize.height avec le spectacle keyboard.Then il y aura un champ noir vers le haut après la cacher le clavier. Alors maintenant je dois utiliser un nombre constant.

2.La vue monte après le clavier, c'est bon.Mais la vue redescend quand je tape des caractères ou je clique sur autre TextFiled.Il est bon Si je mets ces TextFields dans un ScrollView.Mais je ne fais pas ' C'est pourquoi.Comme ci-dessous l'image: enter image description here

Toute aide serait grandement appréciée.

Répondre

0

Votre méthode « viewDidLoad » contient une erreur, vous devez ajouter et ne pas supprimer l'observateur, vous devez l'enlever dans « viewWillDisappear »

override func viewDidLoad() { 
    super.viewDidLoad()    
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)  
} 
+0

Je l'ai fait ces factly.I n'a tout simplement pas passé eux.Becasue ce n'est pas la clé pour ma question.Merci. – WangYang