视图元素未显示在ViewController中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (63)

我有这个奇怪的问题,我试图只是以编程方式显示文本字段,按钮,但在我的视图中没有显示任何内容。我已经调试过,并设置了断点,奇怪的是我得到的点都被执行了,但我的设备上仍然没有任何渲染。

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
    }

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
    }

}

奇怪的是,页脚视图正在显示,但是没有任何按钮、文本字段或其他任何东西出现。

提问于
用户回答回答于

可能是因为你将堆栈顶约束设置为此处视图的底部:

stackView.anchor(top: view.safeAreaLayoutGuide.bottomAnchor

变成:

stackView.anchor(top: view.safeAreaLayoutGuide.topAnchor

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问5 回答
  • uncle_light

    5 粉丝518 提问4 回答
  • o o

    4 粉丝495 提问4 回答
  • 学生

    8 粉丝476 提问3 回答

扫码关注云+社区

领取腾讯云代金券

玩转腾讯云 有奖征文活动