Ios Xcode 12.2安全密码文本字段异常行为
我创建了一个注册视图控制器,其中包含3个文本字段,分别用于电子邮件、用户名和密码。以下是整个视图控制器文件:Ios Xcode 12.2安全密码文本字段异常行为,ios,uikit,uitextfield,Ios,Uikit,Uitextfield,我创建了一个注册视图控制器,其中包含3个文本字段,分别用于电子邮件、用户名和密码。以下是整个视图控制器文件: // // SignupViewController.swift // bounce_frontend // // Created by Sebastian Fay on 11/21/20. // import UIKit class SignupViewController: UIViewController { struct Constants {
//
// 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
}
}
它们的布局如下:
在模拟器中,当我单击密码文本字段时,文本字段变为黄色并显示“强密码”,同时还隐藏输入的文本:。
但是,我也意识到,如果我在电子邮件和用户名文本字段之前单击密码文本字段,文本字段的行为与预期的一样:
我读过其他帖子,说XCode推断视图的目的可能是从文件名和变量名注册,这可能会导致错误,但我还没有找到解决此特定情况的方法。谢谢 我的第一印象是,视图控制器中有一个逻辑,它有副作用。超过你的控制器代码 覆盖文本字段内容的附加“密码”标签是预期行为,因为您激活了该选项,称为密码自动填充 要完全激活此功能,您必须启用应用程序的关联域
如果不可能,那么只需将textContentType设置为.password,并将isSecureTextEntry设置为true。我的第一印象是,视图控制器中有一个具有副作用的逻辑。超过你的控制器代码 覆盖文本字段内容的附加“密码”标签是预期行为,因为您激活了该选项,称为密码自动填充 要完全激活此功能,您必须启用应用程序的关联域 如果不可能,只需将textContentType设置为.password,并将isSecureTextEntry设置为true