Swift UILabel未显示在视图中

Swift UILabel未显示在视图中,swift,xcode,Swift,Xcode,我刚开始学习如何在不使用故事板的情况下编程,所以请把我当作初学者。。。即使我的图像显示在屏幕上,标签也不会。。。我有点困惑为什么会这样,我感谢你的帮助。再说一次,我是初学者,不要介意这是一个愚蠢的错误 import UIKit class LocationRequestController : UIViewController { let imageView: UIImageView = { let iconImageView = UIImageView()

我刚开始学习如何在不使用故事板的情况下编程,所以请把我当作初学者。。。即使我的图像显示在屏幕上,标签也不会。。。我有点困惑为什么会这样,我感谢你的帮助。再说一次,我是初学者,不要介意这是一个愚蠢的错误

import UIKit

class LocationRequestController : UIViewController
{
    let imageView: UIImageView =
    {
        let iconImageView = UIImageView()
        iconImageView.contentMode = .scaleAspectFit
        iconImageView.image = UIImage(named: "blue-pin")
        return iconImageView
    }()

    let allowLocationLabel : UILabel =
    {
        let label = UILabel()
        let attributedText = NSMutableAttributedString(string: "Allow Location\n", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 24)])
        attributedText.append(NSAttributedString(string: "Please enable location services For The Map To Work!", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]))

        label.numberOfLines = 0
        label.textAlignment = .center
        label.attributedText = attributedText

        return label
    }()
    override func viewDidLoad()
    {
        super.viewDidLoad()
        configureViewAppearance()
    }

    func configureViewAppearance()
    {
        view.backgroundColor = .white

        view.addSubview(imageView)
        imageView.anchor(top: view.topAnchor, left: nil, bottom: nil, right: nil, paddingTop: 140, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 200, height: 200)
        imageView.centerX(inView: view)

        view.addSubview(allowLocationLabel)
        allowLocationLabel.anchor(top: imageView.bottomAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 32, paddingLeft: 32, paddingBottom: 0, paddingRight: 32, width: 0, height: 0)
        allowLocationLabel.centerX(inView: view)
    }
}

再次感谢

您只需添加一行
标签。translatesAutoResizengMaskintoConstraints

let allowLocationLabel : UILabel =
    {
        let label = UILabel()
        let attributedText = NSMutableAttributedString(string: "Allow Location\n", attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 24)])
        attributedText.append(NSAttributedString(string: "Please enable location services For The Map To Work!", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 16)]))

        label.numberOfLines = 0
        label.textAlignment = .center
        label.attributedText = attributedText
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()
这就是问题所在:


文本颜色自动设置为.white,这就是它似乎没有显示的原因!谢谢大家的帮助

您是否检查了控制台是否存在损坏的约束?另外,在使用自动布局时,您需要将元素标记为
translatesAutoResizengMaskintoConstraints
为false以遵循自动布局更改Shey Joshua,感谢您尝试帮助我解决此问题,如您所见,我发现了错误。我会删除这个问题,因为它是一个糟糕的问题,但堆栈溢出不允许我这样做。。。所以,请不要失望,它仍然不起作用。。。。标签未显示在图像下方。您使用设置约束?我找到了答案,我正在使用UIView的扩展,然后通过调用该函数中的函数来添加约束谢谢您的帮助,我非常感谢!