【发布时间】:2020-11-26 00:52:23
【问题描述】:
我创建了一个注册视图控制器,其中包含用于电子邮件、用户名和密码的 3 个文本字段。这是整个视图控制器文件:
//
// SignupViewController.swift
// bounce_frontend
//
// Created by Sebastian Fay on 11/21/20.
//
import UIKit
class SignupViewController: UIViewController {
struct Constants {
static let cornerRadius: CGFloat = 5.0
}
private let emailField: UITextField = {
let field = UITextField()
field.placeholder = "Email"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
field.layer.borderWidth = 1.0
field.layer.borderColor = UIColor.secondaryLabel.cgColor
return field
}()
private let usernameField: UITextField = {
let field = UITextField()
field.placeholder = "Username"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
field.layer.borderWidth = 1.0
field.layer.borderColor = UIColor.secondaryLabel.cgColor
return field
}()
private let passwordField: UITextField = {
let field = UITextField()
field.isSecureTextEntry = true
field.textContentType = .oneTimeCode
field.placeholder = "Password"
field.returnKeyType = .next
field.leftViewMode = .always
field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
field.autocapitalizationType = .none
field.autocorrectionType = .no
field.layer.masksToBounds = true
field.layer.cornerRadius = Constants.cornerRadius
field.backgroundColor = .secondarySystemBackground
field.layer.borderWidth = 1.0
field.layer.borderColor = UIColor.secondaryLabel.cgColor
return field
}()
private let createAccountButton: UIButton = {
let button = UIButton()
button.setTitle("Create account", for: .normal)
button.layer.masksToBounds = true
button.layer.cornerRadius = Constants.cornerRadius
button.backgroundColor = .systemRed
button.setTitleColor(.white, for: .normal)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
createAccountButton.addTarget(self, action: #selector(didTapCreateAccountButton), for: .touchUpInside)
emailField.delegate = self
usernameField.delegate = self
passwordField.delegate = self
addSubviews()
view.backgroundColor = .systemBackground
// Do any additional setup after loading the view.
}
private func addSubviews() {
view.addSubview(emailField)
view.addSubview(usernameField)
view.addSubview(passwordField)
view.addSubview(createAccountButton)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
emailField.frame = CGRect(x: 20, y: view.safeAreaInsets.top + 100, width: view.width-40, height: 52)
usernameField.frame = CGRect(x: 20, y: emailField.bottom + 10, width: view.width-40, height: 52)
passwordField.frame = CGRect(x: 20, y: usernameField.bottom + 10, width: view.width-40, height: 52)
createAccountButton.frame = CGRect(x: 20, y: passwordField.bottom + 10, width: view.width-40, height: 52)
}
@objc private func didTapCreateAccountButton() {
print("creating account")
emailField.resignFirstResponder()
usernameField.resignFirstResponder()
passwordField.resignFirstResponder()
guard let userEmail = emailField.text, !userEmail.isEmpty, let userUsername = usernameField.text, !userUsername.isEmpty, let userPassword = passwordField.text, !userPassword.isEmpty, userPassword.count >= 8 else {
return
}
// check if the email and username are available
AuthManager.shared.emailUsernameAvailable(email: userEmail, username: userUsername) { areAvailable in
if areAvailable {
print("username and email are available")
} else {
print("username and email are NOT available")
}
}
}
}
extension SignupViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == emailField {
usernameField.becomeFirstResponder()
} else if textField == usernameField {
passwordField.becomeFirstResponder()
} else if textField == passwordField {
didTapCreateAccountButton()
}
return true
}
}
它们的布局如下: fresh view controller
在模拟器中,当我点击密码文本字段时,文本字段变为黄色并显示“强密码”,同时隐藏输入的文本:。 text typed into password field
但是,我也意识到,如果我在电子邮件和用户名 textField 之前单击密码 textField,则 textField 会按预期运行: text typed into password field before others
我读过其他帖子说 XCode 推断您的视图的目的可能是从文件名和变量名注册,这可能导致错误,但我无法找到针对这种特定情况的修复.谢谢!
【问题讨论】:
标签: ios uikit uitextfield