【问题标题】:View design not showing in ViewController视图设计未显示在 ViewController 中
【发布时间】:2019-05-30 17:01:57
【问题描述】:

我正在尝试创建 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
}()
}

这是我的视图控制器:

import UIKit

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


}

如您所见,我确实设置了所有内容,并且更改了按钮的外观并选择了背景颜色,但它不会显示在 ViewController 上。 我想将ViewViewController 分开,因为我真的需要这个应用程序的灵活性,但它就是行不通。 有什么想法吗?

【问题讨论】:

  • self.view = loginVC() 中,不应该是self.view = LoginView() 吗?
  • 是的,我刚改了,忘记更新了。我现在会的。
  • 嗨,我将背景颜色更改为黑色,您的 uiview 背景在整个屏幕上都发生了变化,对吗?
  • 请检查您的 UIColor 代码。?我只改变颜色,除了都一样,它对我有用
  • 你必须在 didLoad() 中做这件事

标签: ios swift uiview uiviewcontroller uistackview


【解决方案1】:

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

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

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

编辑:

尝试用UIColor 替换UIColor.appColors,看看它是否给出了预期的结果。这是我在您的代码中更改的唯一内容,以使其完美运行。

【讨论】:

  • 是的,我已经改了,但还是一样。
  • 我得到的结果如屏幕截图所示。是否还有其他预期?
  • 不,这是我想要的,但它不起作用。
  • @Augusto 您没有在 viewDidLoad() 中设置控制器的视图。苹果为此提供了一个单独的方法 - loadView()。
  • 感谢您的解释。我看不出它不起作用的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-19
相关资源
最近更新 更多