因此,我在Xcode11中创建了一个新项目,将AppDelegate设置为新的VC,并注释了xxx场景委托中存在的代码中没有UIKit部分:
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中设置背景色,
import UIKit
class MainVC : UIViewController {
override func viewDidLoad() {
view.backgroundColor = .red
self.view.backgroundColor = .blue
print("main Screen showing")
ConfigureUI()
setupUI()
}
但是结果是Simulator中出现了blackScreen。甚至从其他项目中获取代码也无济于事... 我之前在其他Xcode版本中已经做到这一点,并且应该可以工作。有任何想法吗?
PS:该应用程序进入ViewController,可以在控制台中打印,但屏幕为黑色。
答案 0 :(得分:1)
并注释了xxx场景委托中存在的代码不包含UIKit部分
您一定不能那样做。您需要在正确的位置放置您的代码。如果您在Xcode 11中创建一个新项目,则此代码将无效:
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属性不是应用程序的窗口,因此您所做的事情没有意义。现在,该窗口属于场景委托。那是您需要创建窗口并设置其根视图控制器的地方。
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()
}
}