阅读后https://medium.com/ios-os-x-development/ios-start-an-app-without-storyboard-5f57e3251a25
我已执行以下步骤
Main.storyboard和LaunchScreen.storyboardMain.storyboard删除对和LaunchScreen.storyboard来自的引用Info.plist这是上述步骤的一些屏幕截图
在AppDelegate.swift,我做了以下修改
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let viewController = ViewController()
viewController.view.backgroundColor = UIColor.red
window!.rootViewController = viewController
window!.makeKeyAndVisible()
print("application executed")
return true
}
}
Run Code Online (Sandbox Code Playgroud)
当我启动模拟器时,控制台上会打印“已执行的应用程序”。
我预计应用程序启动后会看到全屏红色。但是,我只能看到整个黑屏。
我可以知道我还错过了哪些步骤吗?
您有场景生命周期,因此应该在SceneDelegate.
这是AppDelegate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
}
Run Code Online (Sandbox Code Playgroud)
现在这里是SceneDelegate
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let viewController = ViewController()
viewController.view.backgroundColor = UIColor.red
let window = UIWindow(windowScene: windowScene)
window.rootViewController = viewController
self.window = window
window.makeKeyAndVisible()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
429 次 |
| 最近记录: |