Ios 自定义按钮在导入后未显示在我的viewController中

Ios 自定义按钮在导入后未显示在我的viewController中,ios,swift,Ios,Swift,我正在尝试创建一个自定义按钮并在viewController中导入,以减少其中的代码。然而,它不起作用。有人能给我一些提示吗 尽管ButtonView类已正确实例化,但按钮未显示 按钮查看快捷键 import UIKit final class ButtonView: UIButton { // MARK: - Init override init(frame: CGRect) { super.init(frame: frame) se

我正在尝试创建一个自定义按钮并在viewController中导入,以减少其中的代码。然而,它不起作用。有人能给我一些提示吗

尽管ButtonView类已正确实例化,但按钮未显示

按钮查看快捷键

    import UIKit

final class ButtonView: UIButton {

    // MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupLayout()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Private functions
    private func setupLayout() {
        setTitle("Log In", for: .normal)
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        setTitleColor(.white, for: .normal)
        backgroundColor = UIColor(red: 94/255, green: 162/255, blue: 58/255, alpha: 1)
        layer.cornerRadius = 10
        translatesAutoresizingMaskIntoConstraints = false
    }
}
class HomeController: UIViewController {

    //MARK: - Properties
    var delegate: ViewControllerDelegate?

    //MARK: - Views
    private let button = ButtonView()

    //MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        setupLayout()
        configureNavigationBar()
    }

    fileprivate func setupLayout() {
        view.backgroundColor = .white

        view.addSubview(button)

        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.widthAnchor.constraint(equalToConstant: 150).isActive = true
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100).isActive = false

}
...
HomeController.swift

    import UIKit

final class ButtonView: UIButton {

    // MARK: - Init
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupLayout()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Private functions
    private func setupLayout() {
        setTitle("Log In", for: .normal)
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        setTitleColor(.white, for: .normal)
        backgroundColor = UIColor(red: 94/255, green: 162/255, blue: 58/255, alpha: 1)
        layer.cornerRadius = 10
        translatesAutoresizingMaskIntoConstraints = false
    }
}
class HomeController: UIViewController {

    //MARK: - Properties
    var delegate: ViewControllerDelegate?

    //MARK: - Views
    private let button = ButtonView()

    //MARK: - Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        setupLayout()
        configureNavigationBar()
    }

    fileprivate func setupLayout() {
        view.backgroundColor = .white

        view.addSubview(button)

        button.heightAnchor.constraint(equalToConstant: 50).isActive = true
        button.widthAnchor.constraint(equalToConstant: 150).isActive = true
        button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100).isActive = false

}
...

垂直放置按钮的约束未激活,请将其更改为
true

button.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 100).isActive = true

这个代码看起来不错。你需要检查你如何显示
HomeController
是否正确。
button.topAnchor.constraint(equalTo:view.safeAreaLayoutGuide.topAnchor,常量:100)。isActive=false
为什么它是false?按钮约束不正确哦,我在iPhone 8 plus上试用过。真丢脸哈哈哈