ios - 删除主 Storyboard 后。以编程方式添加 StoryBoard 并具有向后兼容性
问题描述
我的应用程序目标是 iOS 10.0 一旦我更改了目标,我就会在 SceneDelegate 文件中收到一堆错误。为了向后兼容,我为 Scene Delegate 类添加了“@available(iOS 13.0, *)”。
我从一个 OnboardingController 开始。这完全是程序化的。所以我删除了 MainStoryboard 并将其从 info 中的“Main Interface”和“Application Scene Manifest”中删除。
现在,我必须在 AppDelegate 和 SceneDelegate 中设置 rootView。如果我没有在 SceneDelegate 中设置窗口,我只会在 iOS 13.0+ 设备中获得黑屏,如果我没有在 AppDelegate 中设置,我只会在 <13.0 设备中获得黑屏 因为我在 Viewcontroller 中调用这两个文件我的 viewdidload()被调用两次。
以下是我的 AppDelegate
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 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
window?.rootViewController = viewController
window?.makeKeyAndVisible()
return true
}
}
以下是我的 SceneDelegate
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
window?.rootViewController = viewController
window?.makeKeyAndVisible()
}
...
}
我错过了什么吗?
解决方案
This can be found easily by searching, but here is an example...
Note: reference to SO user Matt
SceneDelegate.swift
import UIKit
// entire class is iOS 13+
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeCollectionVC") as! HomeCollectionVC
window?.rootViewController = viewController
window?.makeKeyAndVisible()
}
func sceneDidDisconnect(_ scene: UIScene) {
}
func sceneDidBecomeActive(_ scene: UIScene) {
}
func sceneWillResignActive(_ scene: UIScene) {
}
func sceneWillEnterForeground(_ scene: UIScene) {
}
func sceneDidEnterBackground(_ scene: UIScene) {
}
}
AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window : UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?)
-> Bool {
if #available(iOS 13, *) {
// do only pure app launch stuff, not interface stuff
} else {
self.window = UIWindow()
let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "navC") as! UINavigationController
window?.rootViewController = viewController
window?.makeKeyAndVisible()
}
return true
}
// MARK: UISceneSession Lifecycle
// iOS 13+ only
@available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
// iOS 13+ only
@available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
}
推荐阅读
- c# - 如何使用列名而不是c#中的列号仅检索csv的几列数据
- python - /usr/bin/python3:错误的解释器:符号链接的级别太多
- php - 在 PHP 中获取 XML 模式值
- javascript - 从Javascript中的对象数组中按键查找重复值
- c# - c# 切换到下一页时gridview中的分页问题
- node.js - npm 错误!arg 参数以非 ascii 破折号开头,这可能无效:[ '- g', 'appium' ]
- react-native - React Native Navigation v2 sideMenu 无法导航到屏幕
- javascript - html中div内部的新行自动修剪
- php - 如何记录 ZF1 助手以在我的 IDE 中自动完成
- plsql - PLSQL:在页面加载时有效地显示来自多个表的数据