我以编程方式创建的导航控制器不像示例中那样出现
Posted
技术标签:
【中文标题】我以编程方式创建的导航控制器不像示例中那样出现【英文标题】:navigation controller that i create programmatically not appearing as in examples 【发布时间】:2019-11-17 03:54:38 【问题描述】:我正在尝试在我的 swift 项目中创建一个导航控制器。这个时候我只有一个 View Controller,我到处找,想创建导航控制器。我用这些来尝试让它工作: Swift – Instantiating a navigation controller without storyboards in App Delegate
Creating a navigationController programmatically (Swift)
https://www.youtube.com/watch?v=7ZQMv1okhmI
但这些都不适合我。
这是我的 appDelegate:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
let mainVC = ViewController() as UIViewController
let navigationController = UINavigationController(rootViewController: mainVC)
navigationController.navigationBar.isTranslucent = false
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
return true
// MARK: UISceneSession Lifecycle
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)
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.
这是我的 ViewController:
import UIKit
class ViewController: UIViewController
override func viewDidLoad()
super.viewDidLoad()
view.backgroundColor = .green
这是出现在我的模拟器中的内容:
我只是好奇我做错了什么?我查找了几种以编程方式创建导航控制器的方法,它们都以与我上面提供的示例相同的方式结束。
如果还有什么我可以帮助的,请询问,并且请不要标记为重复,因为我知道这个问题之前已经被问过,但这些问题似乎都不适合我。
【问题讨论】:
【参考方案1】:如果您使用的是 Xcode 11 和 ios 13,则需要在 SceneDelegate.swift
中编写相同的代码。 Apple 将UISceneDelegate
添加到 iOS 13 的主要原因是为多窗口应用程序创建一个良好的入口点。
如果你不打算支持多窗口,你可以从 Info.plist 中删除 SceneDelegate.swift
文件删除 UIApplicationSceneManifest
键并从 AppDelegate.swift
中删除 func application(_: configurationForConnecting:, options:)
和 func application(_:, didDiscardSceneSessions:)
方法
在donnywalls - Understanding the iOS 13 Scene Delegate了解更多关于场景代理的信息
【讨论】:
以上是关于我以编程方式创建的导航控制器不像示例中那样出现的主要内容,如果未能解决你的问题,请参考以下文章
如果以编程方式创建 UIPopoverController 中的导航栏看起来很难看
以编程方式创建 UINavigationController