iOS Swift修改程序入口AppDelegate

2022-06-29  本文已影响0人  Lee坚武

1.项目启动项配置
(文中代码为swift3.0)
1.非navigationController根视图创建

var window: UIWindow?
var viewController: UIViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow.init(frame: UIScreen.main.bounds)
        let vc = ViewController()
        self.viewController = vc
        self.window?.rootViewController = self.viewController
        self.window?.makeKeyAndVisible()
        return true
    }

2.包含navigationController根视图创建
方法1

var window: UIWindow?
var viewController: UINavigationController?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        self.window = UIWindow.init(frame: UIScreen.main.bounds)
        let vc = ViewController()
        self.viewController = UINavigationController.init(rootViewController: vc)
        self.window?.rootViewController = self.viewController
        self.window?.makeKeyAndVisible()
        return true
}

方法2 (swift-4.0)
AppDelegate.swift 中

window = UIWindow(frame: UIScreen.main.bounds)
let rootVC = ViewController()
let navrootVC = UINavigationController(rootViewController: rootVC)
window?.rootViewController = navrootVC
window?.makeKeyAndVisible()

SceneDelegate.swift 中

guard let winScene = (scene as? UIWindowScene) else { return }
        
let rootvc = ViewController()
let rootNav = UINavigationController(rootViewController: rootvc)
 
let win = UIWindow(windowScene: winScene)
win.rootViewController = rootNav
win.makeKeyAndVisible()
window = win

3.用storyboard 在appdelegate里面跳转控制器的代码

@icelovery func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let viewController = storyboard.instantiateViewControllerWithIdentifier("storybord名字")
    self.window?.rootViewController = viewController
    self.window?.makeKeyAndVisible()
    return true
}
上一篇下一篇

猜你喜欢

热点阅读