gpt4 book ai didi

ios - 查看元素未显示在 ViewController 中

转载 作者:行者123 更新时间:2023-11-28 14:22:17 24 4
gpt4 key购买 nike

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

在 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
}

在登录 View Controller 中:

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
}

}

奇怪的是,页脚 View 出现了,但按钮、文本框或其他任何东西都没有出现。

有没有人见过这样的东西?我是否遗漏了一些明显的东西?

最佳答案

可能是因为你在这里设置了stack top constraint to the bottom

stackView.anchor(top: view.safeAreaLayoutGuide.bottomAnchor

stackView.anchor(top: view.safeAreaLayoutGuide.topAnchor

关于ios - 查看元素未显示在 ViewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com