美文网首页
Xcode11 iOS13 新建Swift项目(黑屏)

Xcode11 iOS13 新建Swift项目(黑屏)

作者: 文子飞_ | 来源:发表于2020-06-08 15:52 被阅读0次

    不使用StoryBoard加载Main

     info.plist文件中 删除键值对
    
     StoryBoardName : Main
    
    image.png

    第一步:AppDelegate添加window,用于 iOS 13 以下加载rootViewController

    var window: UIWindow?
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
            
            // iOS 13 以下加载rootViewController
            if #available(iOS 13.0, *)  {
               
             } else {
                self.window = UIWindow(frame: UIScreen.main.bounds)
                
                 let webVC = LFWebVC()
                self.window?.rootViewController = webVC
                
                let urlString = "https://www.baidu.com/"
                let url = URL.init(string: urlString)
                webVC.openURL(url: url!)
                
                self.window?.makeKeyAndVisible()
    
             }
            return true
        }
    

    第二步:SceneDelegate用于 iOS 13加载rootViewController

    var window: UIWindow?
        
        @available(iOS 13.0, *)
        func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
            
            //guard let _ = (scene as? UIWindowScene) else { return }
            
            // iOS 13加载rootViewController
            if #available(iOS 13.0, *)  {
                self.window = UIWindow(windowScene: scene as! UIWindowScene)
            } else {
                self.window = UIWindow(frame: UIScreen.main.bounds)
            }
            
           
            self.window?.backgroundColor =  UIColor.red
            
            let webVC = LFWebVC()
            self.window?.rootViewController = webVC
            self.window?.makeKeyAndVisible()
            
            let urlString = "https://www.baidu.com/"
            guard let url = URL.init(string: urlString) else { return }
            webVC.openURL(url: url)
            
        }
    

    第三步:scene相关方法里加上@available(iOS 13.0, *)

        @available(iOS 13.0, *)
        func sceneDidBecomeActive(_ scene: UIScene) {
            // Called when the scene has moved from an inactive state to an active state.
            // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
        }
    
        @available(iOS 13.0, *)
        func sceneWillResignActive(_ scene: UIScene) {
            // Called when the scene will move from an active state to an inactive state.
            // This may occur due to temporary interruptions (ex. an incoming phone call).
        }
    
        @available(iOS 13.0, *)
        func sceneWillEnterForeground(_ scene: UIScene) {
            // Called as the scene transitions from the background to the foreground.
            // Use this method to undo the changes made on entering the background.
        }
    
        @available(iOS 13.0, *)
        func sceneDidEnterBackground(_ scene: UIScene) {
            // Called as the scene transitions from the foreground to the background.
            // Use this method to save data, release shared resources, and store enough scene-specific state information
            // to restore the scene back to its current state.
        }
    

    以上设置完毕即可运行iOS13及其以下系统

    相关文章

      网友评论

          本文标题:Xcode11 iOS13 新建Swift项目(黑屏)

          本文链接:https://www.haomeiwen.com/subject/susrtktx.html