Xcode 11和iOS13,使用UIKIT不能更改UIViewController的背景颜色 [英] Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController

查看:88
本文介绍了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()

    }

但是结果是Simulator中出现了blackScreen.甚至从其他项目中获取代码也无济于事...我以前在其他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:该应用程序进入ViewController,可以在控制台中打印,但屏幕为黑色.

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

推荐答案

并注释了xxx场景委托中存在的代码不包含UIKit部分

and commented the code present in xxx scene delegate to not have the UIKit part

您一定不能那样做.需要将您的代码放在正确的位置.如果您在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的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆