ios - View 设计未显示在 ViewController 中

标签 ios swift uiview uiviewcontroller uistackview

我正在尝试创建一个 UIView 并将其设置为 ViewController 的 View 。 我确实设置了它,但它不会正确显示。例如,我为未显示的 View 选择了 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
}()
}

这是我的 View Controller :

import UIKit

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


}

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

最佳答案

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 并查看它是否提供预期的结果。这是我在您的代码中所做的唯一更改,以使其完美运行。

关于ios - View 设计未显示在 ViewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56382579/

相关文章:

ios - 我如何构建独立的应用程序,用户可以在没有 Expo 客户端的情况下打开它

ios - 将 NSMutableDictionary 设置为 nil 是否会释放对其所有内容的引用?

ios - didSelectRowAtIndexPath 不工作

ios - swift 中文本字段的前景色

ios - 导航 Controller :willShowViewController:animated: is not called after popToRootViewControllerAnimated in presented view controller

uiview - 我的 UIView 动画也可以有交互式过渡吗?

ios - 如何将NSString hexInterval转成NSTimeInterval进行日期转换

ios - 如何在点击 Swift4 时关闭 UIView

iPhone iOS 如何使 UIRotationGestureRecognizer 和 UIPinchGestureRecognizer 协同工作以缩放和旋转带有 subview 的 UIView?

iphone - 当用户摇动iPhone时如何显示警报?(即使用户未使用该应用程序也应该可以使用)