简体   繁体   English

视图元素未显示在ViewController中

[英]View elements not showing up in ViewController

I have this weird issue where I am trying to just display textfields, buttons, programmatically, and nothing is showing up in my view. 我有一个奇怪的问题,我试图以编程方式仅显示文本字段,按钮,而我的视图中什么都没有显示。 I've debugged, and set breakpoints and oddly I'm getting that the points are all being executed, but still nothing renders on my device. 我已经调试,设置了断点,奇怪的是,这些点都已被执行,但是在我的设备上仍然没有任何渲染。

in AppDelegate: 在AppDelegate中:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        let loginVC = LoginViewController()
        let navController = UINavigationController(rootViewController: loginVC)
        window!.rootViewController = navController
        window!.makeKeyAndVisible()

        return true
    }

In LoginViewController: 在LoginViewController中:

import UIKit

class LoginViewController: UIViewController {

    let footerView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.mainGreen()
        return view
    }()

    let emailTextField: UITextField = {
        let tf = UITextField()
        tf.attributedPlaceholder = NSAttributedString(string: "Email", attributes: [NSAttributedStringKey.foregroundColor: UIColor.mainWhite()])
        tf.textAlignment = .center
        tf.textAlignment = .center
        tf.textColor = .white
        tf.backgroundColor = UIColor.mainGreen()
        tf.borderStyle = .roundedRect
        tf.font = UIFont.systemFont(ofSize: 14)
        return tf
    }()

    let passwordTextField: UITextField = {
        let tf = UITextField()
        tf.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSAttributedStringKey.foregroundColor: UIColor.mainWhite()])
        tf.textAlignment = .center
        tf.textColor = .white
        tf.isSecureTextEntry = true
        tf.backgroundColor = UIColor.mainGreen()
        tf.borderStyle = .roundedRect
        tf.font = UIFont.systemFont(ofSize: 14)
        return tf
    }()

    let loginButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Login", for: .normal)
        button.setTitleColor(UIColor.mainGreen(), for: .normal)
        button.backgroundColor = UIColor.mainWhite()
        button.layer.cornerRadius = 5
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        button.isEnabled = true
        return button
    }()

    let dontHaveAccountButton: UIButton = {
        let button = UIButton(type: .system)
        let attributedTitle = NSMutableAttributedString(string: "Don't have an account? ", attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.mainGreen()])
        attributedTitle.append(NSAttributedString(string: "Get Started", attributes: [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 14),
                                                                                  NSAttributedStringKey.foregroundColor: UIColor.mainGreen()]))
        button.setAttributedTitle(attributedTitle, for: .normal)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.mainWhite()

        view.addSubview(footerView)
        footerView.anchor(top: nil, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 60)

        let stackView = UIStackView(arrangedSubviews: [emailTextField, passwordTextField, loginButton])
        stackView.axis = .vertical
        stackView.spacing = 10
        stackView.distribution = .fillEqually

        view.addSubview(stackView)

        stackView.anchor(top: view.safeAreaLayoutGuide.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 40, paddingLeft: 40, paddingBottom: 0, paddingRight: 40, width: 0, height: 140)

        view.addSubview(dontHaveAccountButton)
        dontHaveAccountButton.anchor(top: stackView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 12, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 50)

    }

    override func viewWillAppear(_ animated: Bool) {
        navigationController?.isNavigationBarHidden = true
    }

}

Oddly enough, the footer view is showing up, but none of the buttons, textfields, or anything else is. 奇怪的是,显示了页脚视图,但是没有按钮,文本字段或其他任何东西。

Has anyone ever seen something like this? 有没有人看过这样的东西? Am I missing something obvious? 我是否缺少明显的东西?

May be because you set the stack top constraint to the bottom of the view here 可能是因为您在此处将堆栈顶部约束设置为视图的底部

stackView.anchor(top: view.safeAreaLayoutGuide.bottomAnchor

do

stackView.anchor(top: view.safeAreaLayoutGuide.topAnchor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM