2017-01-15 27 views
0

我找不到如何在鍵盤出現時向上移動UIView的好方法。我的解決方案,現在是這樣的:如何在鍵盤出現時對UIView進行排序?

import UIKit 

class LoginViewControllerbybys: UIViewController, UITextFieldDelegate { 

    // Create username and password text boxes 
    let emailTextField: UITextField = { 
     let field = UITextField() 
     field.placeholder = "EMAIL" 
     field.font = UIFont(name: "Helvetica Neue", size: 14) 
     field.background = UIImage(named: "divider") 
     field.autocapitalizationType = .none 
     field.autocorrectionType = .no 
     field.spellCheckingType = .no 
     field.translatesAutoresizingMaskIntoConstraints = false 
     return field 
    }() 

    let passwordTextField: UITextField = { 
     let field = UITextField() 
     field.placeholder = "PASSWORD" 
     field.font = UIFont(name: "Helvetica Neue", size: 14) 
     field.background = UIImage(named: "divider") 
     field.autocapitalizationType = .none 
     field.autocorrectionType = .no 
     field.spellCheckingType = .no 
     field.isSecureTextEntry = true 
     field.translatesAutoresizingMaskIntoConstraints = false 
     return field 
    }() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.backgroundColor = .white // sets background colour to white 

     //Adding notifies on keyboard appearing 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
     NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

     // Adding Logo in the centre-top of the screen 
     let logo: UIImageView = { 
      let image = UIImage(named: "logo") 
      let imageView = UIImageView(image: image) 
      imageView.translatesAutoresizingMaskIntoConstraints = false 
      return imageView 
     }() 

     func logoView() { 
      logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
      logo.heightAnchor.constraint(equalToConstant: 75).isActive = true 
      logo.widthAnchor.constraint(equalToConstant: 75).isActive = true 
      logo.topAnchor.constraint(equalTo: view.topAnchor, constant: 30).isActive = true 
     } 

     func emailTextFieldView() { 
      emailTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
      emailTextField.heightAnchor.constraint(equalToConstant: 35).isActive = true 
      emailTextField.widthAnchor.constraint(equalToConstant: 310).isActive = true 
      emailTextField.topAnchor.constraint(equalTo: logo.bottomAnchor, constant: 50).isActive = true 
     } 

     func passwordTextFieldView() { 
      passwordTextField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
      passwordTextField.heightAnchor.constraint(equalToConstant: 35).isActive = true 
      passwordTextField.widthAnchor.constraint(equalToConstant: 310).isActive = true 
      passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 30).isActive = true 
     } 

     // Create Sign In button 
     let signInButton: UIButton = { 
      let button = UIButton() 
      button.setImage(#imageLiteral(resourceName: "goButton"), for: .normal) 
      button.frame = CGRect(x: 0, y: 597, width: 375, height: 70) 
      button.addTarget(self, action: #selector(signInButtonTapped), for: .touchUpInside) // when tapped on Sign In button, execute function SignInTapped 
      button.translatesAutoresizingMaskIntoConstraints = false 
      return button 
     }() 


     func signInButtonView() { 
      signInButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
      signInButton.heightAnchor.constraint(equalToConstant: 70).isActive = true 
      signInButton.widthAnchor.constraint(equalTo: view.widthAnchor, constant: 0).isActive = true 
      signInButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true 
     } 

     // Show all Subviews 
     self.view.addSubview(logo) 
     self.view.addSubview(signInButton) 
     self.view.addSubview(emailTextField) 
     self.view.addSubview(passwordTextField) 

     // Constraints 
     logoView() 
     signInButtonView() 
     emailTextFieldView() 
     passwordTextFieldView() 
    } 

    func keyboardWillShow(notification: NSNotification) { 

     let keyboardSize: CGSize = ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)! 
     let offset: CGSize = ((notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)! 



     if keyboardSize.height == offset.height { 
      if self.view.frame.origin.y == 0 { 
       UIView.animate(withDuration: 0.1, animations: {() -> Void in 
        self.view.frame.origin.y -= keyboardSize.height 
       }) 
      } 
     } else { 
      UIView.animate(withDuration: 0.1, animations: {() -> Void in 
       self.view.frame.origin.y += keyboardSize.height - offset.height 
      }) 
     } 
    } 

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


} 

然而,這個問題是,它隱藏了一些我的箱子和一個標籤,我看不到他們。我thik我將能夠避免與UIScrollView它,但我不知道如何做到這一點...將不勝感激任何幫助!

回答

相關問題