簡體   English   中英

在 UIStackView 上添加 UITextField 時應用程序崩潰

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM