Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

swift - Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController

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
    }

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()

    }

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: The App gets in the ViewController, I can print in the console, but the screen is black.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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
}

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()                 
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...