本文介绍了Xcode 11 和iOS13,使用UIKIT不能改变UIViewController的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 Xcode11 中创建了一个新项目,将 AppDelegate 设置为我的新 VC,并将 xxx 场景委托中的代码注释为没有 UIKit 部分:

So I created a new project in Xcode11, set the AppDelegate to my new VC and commented the code present in xxx scene delegate to not have the UIKit part:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow()
        window?.makeKeyAndVisible()
        let controller = MainVC()
        window?.rootViewController = controller
        return true
    }

在我的 UIViewController 中,我想设置背景颜色,

In my UIViewController I wanted to set the background colour,

import UIKit

class MainVC : UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = .red
        self.view.backgroundColor = .blue
        print("main Screen showing")
        ConfigureUI()
        setupUI()

    }

但结果是模拟器中的黑屏.甚至从其他项目中获取代码也无济于事......我以前在其他 Xcode 版本中做过这个,应该可以工作.有任何想法吗?

But the result is a blackScreen in Simulator. Not even taking the code from other projects would help...I've done this before in the other Xcode versions and should had work. Any ideas?

PS:App进入ViewController,可以在控制台打印,但是黑屏.

PS: The App gets in the ViewController, I can print in the console, but the screen is black.

推荐答案

你不能那样做.您的代码需要放在正确的位置.如果你在 Xcode 11 中创建一个新项目,这段代码什么也不做:

You mustn't do that. It is your code that needs to go in the right place. If you make a new project in Xcode 11, this code does nothing:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow()
    window?.makeKeyAndVisible()
    let controller = MainVC()
    window?.rootViewController = controller
    return true
}

代码运行,但是window属性不是你应用的窗口,所以你做的事毫无意义.该窗口现在属于 场景委托.这就是您需要创建窗口并设置其根视图控制器的地方.

The code runs, but the window property is not your app's window, so what you're doing is pointless. The window now belongs to the scene delegate. That is where you need to create the window and set its root view controller.

func scene(_ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            self.window = UIWindow(windowScene: windowScene)
            let vc = MainVC()
            self.window!.rootViewController = vc
            self.window!.makeKeyAndVisible()
        }
}

这篇关于Xcode 11 和iOS13,使用UIKIT不能改变UIViewController的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 03:28