[英]App crashes while adding a UITextField on UIStackView
我正在使用 Swift 5 制作一个应用程序,它有两个视图控制器。 第一个,有一个显示第二个 viewController 的按钮。 2º 视图控制器的代码:
import UIKit
import SnapKit
import Firebase
import Hero
@available(iOS 13.0, *)
class RegistroViewController: UIViewController, UINavigationBarDelegate {
//MARK: ATRIBUTOS
var db: Firestore!
// Instancias recursos
let gradientBackground = GradientView()
let vista = RegistroView()
//MARK: viewDidLoad()
override public func viewDidLoad() {
super.viewDidLoad()
// Mostrar fondo gradiente
gradientBackground.mostrarGradientBackground(viewController: self)
// Instanciar Firestore
db = Firestore.firestore()
// Configuracion navigationBar
navBarSettings()
// Mostrar objetos de la vista
vista.mostrarVista(viewController: self)
}
// Funcion para configuracion de navigationBar
func navBarSettings() {
self.navigationController?.isNavigationBarHidden = false
self.navigationController?.navigationBar.backgroundColor = .white
self.navigationController?.navigationBar.isTranslucent = true
self.navigationItem.title = "Registro"
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Cancelar", style: .plain, target: self, action: #selector(self.cancelarRegistro))
}
@objc public func cancelarRegistro(sender: UIBarButtonItem!) {
self.hero.modalAnimationType = .cover(direction: .down)
self.hero.dismissViewController()
}
}
RegistroView代码:
import UIKit
import SnapKit
class RegistroView: UIView {
// Color
let customAzul = UIColor(red: 137/255, green: 177/255, blue: 223/255, alpha: 1)
//scrollView y stackView
let scrollView = UIScrollView()
let stackView = UIStackView()
// Objetos para stackView
let datosPersonales = UILabel()
let nombreTextField = UITextField()
let apellidoTextField = UITextField()
func mostrarVista(viewController: UIViewController){
// Instanciar viewController recibido
let vc = viewController
// Agrego scrollView a la vista del viewController
vc.view.addSubview(scrollView)
// Configuracion scrollView
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentSize = CGSize(width: vc.view.frame.width, height: vc.view.frame.height * 2)
scrollView.snp.makeConstraints { (maker) in
maker.edges.equalToSuperview()
}
// Agrego stackView al scrollView
scrollView.addSubview(stackView)
// Configuracion stackView
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .equalSpacing
stackView.snp.makeConstraints { (maker) in
maker.width.equalToSuperview() // Ancho igual al scrollView
maker.top.equalToSuperview().inset(10) // Posicion Y de arriba: 10px más abajo
maker.bottom.equalToSuperview() // Posicion Y de abajo: igual a scrollView
maker.centerX.equalToSuperview() // stackView centrado horizontalmente
}
// Fuente para labels
let fuente = UIFont.systemFont(ofSize: 25, weight: .bold)
// leftView para textFields
let leftView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 10.0, height: 2.0))
// Configuracion datosPersonales
stackView.addArrangedSubview(datosPersonales)
datosPersonales.translatesAutoresizingMaskIntoConstraints = false
datosPersonales.text = "Datos Personales"
datosPersonales.textColor = UIColor(red: 66/255, green: 135/255, blue: 245/255, alpha: 1)
datosPersonales.font = fuente
// Configuracion nombreTextField
stackView.addArrangedSubview(nombreTextField)
nombreTextField.text = ""
nombreTextField.placeholder = "Nombre"
nombreTextField.autocapitalizationType = .words
nombreTextField.backgroundColor = .white
nombreTextField.layer.cornerRadius = 15
nombreTextField.layer.borderWidth = 4
nombreTextField.layer.borderColor = UIColor(red: 66/255, green: 135/255, blue: 245/255, alpha: 1).cgColor
nombreTextField.leftView = leftView
nombreTextField.leftViewMode = .always
nombreTextField.snp.makeConstraints { (maker) in
maker.width.equalToSuperview().multipliedBy(0.8)
maker.height.equalTo(45)
}
// Configuracion apellidoTextField
/*stackView.addArrangedSubview(apellidoTextField)
apellidoTextField.text = ""
apellidoTextField.placeholder = "Apellido"
apellidoTextField.autocapitalizationType = .words
apellidoTextField.backgroundColor = .white
apellidoTextField.layer.cornerRadius = 15
apellidoTextField.layer.borderWidth = 4
apellidoTextField.layer.borderColor = UIColor(red: 66/255, green: 135/255, blue: 245/255, alpha: 1).cgColor
apellidoTextField.leftView = leftView
apellidoTextField.leftViewMode = .always
apellidoTextField.snp.makeConstraints { (maker) in
maker.width.equalToSuperview().multipliedBy(0.8)
maker.height.equalTo(45)
}*/
}
}
2º viewController 有一个 UIScrollView 作为子视图,scrollView 有一个 verticalStackView。 当stackView包含第一个标签(datosPersonales)和第一个textField(nombreTextField)时,app完美运行; 但是,在向 stackView 添加第二个 textField(声明为 apelidoTextField)并再次运行应用程序时,当我触摸 1º viewController 中的按钮时,应用程序冻结,我无法执行任何其他操作,Xcode 也无法执行显示任何错误或结束执行。
我怎么解决这个问题?
我没有使用故事板
您正在使用相同的leftView
。 对第二个文本字段使用另一个leftView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.