こんにちは
いつも教えてくださる方ありがとうございます。
今日も質問すみません。答えてくださる方いましたらよろしくお願い致します。
1 TextFieldでこの様な表記が出てしまいpasswordのTextFieldの部分が入力できなくなってしまいました。
2 また、一つ上のemailのTextFieldの部分で@マークが押せないという事が起きてしまっています。
どなたか改善方法の方教えていただけますでしょうか。
お手数ですがよろしくお願い致します。
コードがこちらになります。お手数ですがよろしくお願い致します。
swift
1import UIKit 2import Firebase 3import PKHUD 4 5class ViewController: UIViewController { 6 7 @IBOutlet weak var registerButton: UIButton! 8 @IBOutlet weak var emailTextField: UITextField! 9 @IBOutlet weak var passwordTextField: UITextField! 10 @IBOutlet weak var usernameTextField: UITextField! 11 12 @IBAction func tappedRegisterButton(_ sender: Any) { 13 handleAuthToFirebase() 14 } 15 16 @IBAction func tappedAlreadyHaveAccountButton(_ sender: Any) { 17 pushToLoginViewController() 18 } 19 20 override func viewDidLoad() { 21 super.viewDidLoad() 22 setupViews() 23 setupNotificationObserver() 24 } 25 26 override func viewWillAppear(_ animated: Bool) { 27 super.viewWillAppear(animated) 28 29 navigationController?.navigationBar.isHidden = true 30 } 31 32 private func pushToLoginViewController() { 33 let storyBoard = UIStoryboard(name: "Login", bundle: nil) 34 let homeViewController = storyBoard.instantiateViewController(identifier: "LoginViewController") as! LoginViewController 35 navigationController?.pushViewController(homeViewController, animated: true) 36 } 37 38 private func setupViews() { 39 registerButton.isEnabled = false 40 registerButton.layer.cornerRadius = 10 41 registerButton.backgroundColor = UIColor.rgb(red: 255, green: 221, blue: 187) 42 43 emailTextField.delegate = self 44 passwordTextField.delegate = self 45 usernameTextField.delegate = self 46 47 registerButton.addTarget(self, action: #selector(tappedAlreadyHaveAccountButton), for: .touchUpInside) 48 } 49 50 @objc private func tappedRegisterButton() { 51 print("something") 52 } 53 54 private func setupNotificationObserver() { 55 NotificationCenter.default.addObserver(self, selector: #selector(showKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil) 56 NotificationCenter.default.addObserver(self, selector: #selector(hideKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil) 57 } 58 59 private func handleAuthToFirebase() { 60 HUD.show(.progress, onView: view) 61 guard let email = emailTextField.text else { return } 62 guard let password = passwordTextField.text else { return } 63 64 Auth.auth().createUser(withEmail: email, password: password) { (res, err) in 65 if let err = err { 66 print("認証情報の保存に失敗しました。(err)") 67 HUD.hide { (_) in 68 HUD.flash(.error, delay: 1) 69 } 70 return 71 } 72 self.addUserInfoToFirestore(email: email) 73 } 74 } 75 76 // Firestoreにユーザー情報を保存 77 private func addUserInfoToFirestore(email: String) { 78 guard let uid = Auth.auth().currentUser?.uid else { return } 79 guard let name = self.usernameTextField.text else { return } 80 81 let docData = ["email": email, "name": name, "createdAt": Timestamp()] as [String : Any] 82 let userRef = Firestore.firestore().collection("users").document(uid) 83 84 userRef.setData(docData) { (err) in 85 if let err = err { 86 print("Firestoreへの保存に失敗しました。(err)") 87 HUD.hide { (_) in 88 HUD.flash(.error, delay: 1) 89 } 90 return 91 } 92 self.fetchUserInfoFromFirestore(userRef: userRef) 93 } 94 } 95 96 // Firestoreからユーザー情報を取得 97 private func fetchUserInfoFromFirestore(userRef: DocumentReference) { 98 userRef.getDocument { (snapshot, err) in 99 if let err = err { 100 print("ユーザー情報の取得に失敗しました。(err)") 101 HUD.hide { (_) in 102 HUD.flash(.error, delay: 1) 103 } 104 return 105 } 106 107 guard let data = snapshot?.data() else { return } 108 let user = User.init(dic: data) 109 print("ユーザー情報の取得ができました。(user.name)") 110 HUD.hide { (_) in 111 HUD.flash(.success, onView: self.view, delay: 1) { (_) in 112 self.presentToHomeViewController(user: user) 113 } 114 } 115 } 116 } 117 118 private func presentToHomeViewController(user: User) { 119 let storyBoard = UIStoryboard(name: "Home", bundle: nil) 120 let homeViewController = storyBoard.instantiateViewController(identifier: "HomeViewController") as! HomeViewController 121 homeViewController.user = user 122 homeViewController.modalPresentationStyle = .fullScreen 123 self.present(homeViewController, animated: true, completion: nil) 124 } 125 126 @objc func showKeyboard(notification: Notification) { 127 let keyboardFrame = (notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue 128 129 guard let keyboardMinY = keyboardFrame?.minY else { return } 130 let registerButtonMaxY = registerButton.frame.maxY 131 let distance = registerButtonMaxY - keyboardMinY + 20 132 133 let transform = CGAffineTransform(translationX: 0, y: -distance) 134 135 UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: { 136 self.view.transform = transform 137 }) 138 } 139 140 @objc func hideKeyboard() { 141 UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: { 142 self.view.transform = .identity 143 }) 144 } 145 146 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 147 self.view.endEditing(true) 148 } 149 150} 151 152extension ViewController: UITextFieldDelegate { 153 154 func textFieldDidChangeSelection(_ textField: UITextField) { 155 let emaillIsEmpty = emailTextField.text?.isEmpty ?? true 156 let passwordIsEmpty = passwordTextField.text?.isEmpty ?? true 157 let usernameIsEmpty = usernameTextField.text?.isEmpty ?? true 158 159 if emaillIsEmpty || passwordIsEmpty || usernameIsEmpty { 160 registerButton.isEnabled = false 161 registerButton.backgroundColor = UIColor.rgb(red: 255, green: 221, blue: 187) 162 } else { 163 registerButton.isEnabled = true 164 registerButton.backgroundColor = UIColor.rgb(red: 255, green: 141, blue: 0) 165 } 166 } 167 168} 169
調べてみたのですがなかなか解決に辿り着く方法が見当たらなかったので教えてくださると幸いです。
よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー