Xcode 11 & iOS13, 使用 UIKIT 无法改变 UIViewController 的背景颜色
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
所以我在 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
}
在我的 UI 中ViewController 我想设置背景颜色,
import UIKit
class MainVC : UIViewController {
override func viewDidLoad() {
view.backgroundColor = .red
self.view.backgroundColor = .blue
print("main Screen showing")
ConfigureUI()
setupUI()
}
但结果是模拟器黑屏。甚至从其他项目中获取代码也无济于事......
我之前在其他 Xcode 版本中做过这个并且应该有工作。有任何想法吗?
PS:App进入ViewController,控制台可以打印,但是黑屏
and commented the code present in xxx scene delegate to not have the UIKit part
你不能那样做。 您的 代码需要放在正确的位置。如果你在Xcode11新建一个项目,这段代码什么都不做:
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 属性 不是您应用程序的 window,所以您所做的毫无意义。 window 现在属于 场景委托 。这就是您需要创建 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()
}
}
所以我在 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
}
在我的 UI 中ViewController 我想设置背景颜色,
import UIKit
class MainVC : UIViewController {
override func viewDidLoad() {
view.backgroundColor = .red
self.view.backgroundColor = .blue
print("main Screen showing")
ConfigureUI()
setupUI()
}
但结果是模拟器黑屏。甚至从其他项目中获取代码也无济于事...... 我之前在其他 Xcode 版本中做过这个并且应该有工作。有任何想法吗?
PS:App进入ViewController,控制台可以打印,但是黑屏
and commented the code present in xxx scene delegate to not have the UIKit part
你不能那样做。 您的 代码需要放在正确的位置。如果你在Xcode11新建一个项目,这段代码什么都不做:
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 属性 不是您应用程序的 window,所以您所做的毫无意义。 window 现在属于 场景委托 。这就是您需要创建 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()
}
}