ViewController背景颜色不会改变

时间:2019-05-28 18:47:19

标签: ios swift uiviewcontroller

我已经开始了一个新项目,我正在尝试使用MVC和程序化UI来创建它。

我正在尝试设置ViewController的背景色,但不会改变。

我已经编写了程序化UI,但是我更改了ViewController本身的背景颜色,但没有更改view的背景颜色。

这是我的view

import UIKit

class ReportView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.appColors.white
    }

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

这是我的ViewController

import UIKit

class ReportsVC: UIViewController {

    override func loadView() {
        view = ReportView()
    }

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

这是我的AppDelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var navigationController: UINavigationController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow(frame: UIScreen.main.bounds)

        if let window = window{
            let reportsVC = ReportsVC()
            navigationController = UINavigationController(rootViewController: reportsVC)
            window.rootViewController = navigationController
            window.makeKeyAndVisible()
        }

        return true
    }
}

我尝试更改ViewController本身的背景颜色只是为了进行检查,但保持不变。

1 个答案:

答案 0 :(得分:0)

您需要将视图添加到viewcontroller的内容视图中,例如:

self.view.addSubview(ReportView(frame: self.view.frame))

视图需要有一个可见的框架。