视图设计未在ViewController中显示

时间:2019-05-30 17:01:58

标签: ios swift uiview uiviewcontroller uistackview

我正在尝试创建一个UIView并将其设置为ViewController's视图。 我确实进行了设置,但无法正确显示。例如,我为视图选择了backgroundColor,该视图没有显示。我添加了两个按钮,它们确实显示了,但没有达到预期。 这是我的UIView代码:

import UIKit

public final class LoginView: UIView {
public override init(frame: CGRect) {
    super.init(frame: frame)
    self.addSubview(stackView)
    self.backgroundColor = UIColor.appColors.white

    NSLayoutConstraint.activate([
        stackView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
        stackView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
        stackView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
        stackView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
    ])
}

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

public let login: UIButton = {
    let button = UIButton(type: .system)
    button.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17.0)
    button.setTitle("Login", for: .normal)
    button.setTitleColor(UIColor.appColors.white, for: .normal)
    button.backgroundColor = UIColor.appColors.green
    button.contentHorizontalAlignment = .center
    button.layer.cornerRadius = 10
    button.layer.shadowColor = UIColor(white: 0, alpha: 0.25).cgColor
    button.layer.shadowOffset = CGSize(width: 0, height: 2)
    button.layer.shadowOpacity = 1
    button.layer.shadowRadius = 0
    button.layer.masksToBounds = false
    button.clipsToBounds = true

    return button
}()

public let signup: UIButton = {
    let button = UIButton(type: .system)
    button.titleLabel?.font = UIFont(name: "AvenirNext-Regular", size: 17.0)
    button.setTitle("Signup", for: .normal)
    button.setTitleColor(UIColor.appColors.white, for: .normal)
    button.backgroundColor = UIColor.appColors.red
    button.contentHorizontalAlignment = .center
    button.layer.cornerRadius = 10
    button.layer.shadowColor = UIColor(white: 0, alpha: 0.25).cgColor
    button.layer.shadowOffset = CGSize(width: 0, height: 2)
    button.layer.shadowOpacity = 1
    button.layer.shadowRadius = 0
    button.layer.masksToBounds = false
    button.clipsToBounds = true

    return button
}()

private lazy var stackView: UIStackView = {
    let stackView = UIStackView(arrangedSubviews: [self.login, self.signup])
    stackView.axis = .vertical
    stackView.distribution = .fillEqually
    stackView.alignment = .fill
    stackView.spacing = 10.0
    stackView.translatesAutoresizingMaskIntoConstraints = false

    return stackView
}()
}

这是我的ViewController:

import UIKit

class loginVC: UIViewController {
override func loadView() {
    super.loadView()
    self.view = LoginVC()
}
override func viewDidLoad() {
    super.viewDidLoad()
}


}

如您所见,我已经完成了所有设置,并且确实改变了按钮的外观并选择了backgorund颜色,但是它不会显示在ViewController上。 我想将ViewViewController分开,因为我确实需要此应用程序具有灵活性,但无法使用。 有什么想法吗?enter image description here

1 个答案:

答案 0 :(得分:1)

loadView()中,它必须是LoginView(),而不是loginVC(),即

class loginVC: UIViewController {
    override func loadView() {
        super.loadView()
        self.view = LoginView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

enter image description here

编辑:

尝试仅用UIColor.appColors替换UIColor,看看是否能达到预期的效果。这是我在您的代码中所做的唯一更改,以使其能够完美运行。

相关问题