ios - 以编程方式制作 UITabBarController 和 UINavigationController Swift 4

标签 ios swift uinavigationcontroller uitabbarcontroller swift4

我正在尝试以编程方式创建一个 UITabBarController 和一个 UINavigationController。我试过很多教程,但大多数使用的是 Swift 3,它太过时而且不起作用。

AppDelegate.swift 片段:

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)
    let journalVC = JournalTableViewController()
    let navController = UINavigationController(rootViewController: journalVC)
    window?.rootViewController = navController
    window?.makeKeyAndVisible()

    return true
}

JournalTableViewController.swift 片段:

var tabBarCnt = UITabBarController()

override func viewDidLoad() {
    super.viewDidLoad()

    tabBarCnt = UITabBarController()
    tabBarCnt.tabBar.barStyle = .black

    let journalVC = JournalTableViewController()
    journalVC.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)

    tabBarCnt.viewControllers = [journalVC]
    self.view.addSubview(tabBarCnt.view)

}

什么有效:

  • 构建加载到模拟器上

什么不起作用:

  • 加载到模拟器后构建崩溃

  • 崩溃后,错误 Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee6c2ada8) 出现在 JournalTableViewController.swift 的 12 行片段

最佳答案

如果您要组合 UITabBarControllerUINavigationController,那么您需要将标签栏 Controller 作为 Root View Controller 。如果需要,每个选项卡都可以有自己的导航 Controller 。

如果您将导航 Controller 设置为根,那么只要您按下一个新的 View Controller ,标签栏就会消失。通过使选项卡栏成为根,您可以拥有一系列导航层次结构并使用选项卡按钮在它们之间快速切换。

AppDelegate.swift

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

    window = UIWindow(frame: UIScreen.main.bounds)

    let journalVC = JournalViewController()
    let navController = UINavigationController(rootViewController: journalVC)

    let tabBarController = UITabBarController()
    navController.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 0)
    tabBarController.viewControllers = [navController]

    window?.rootViewController = tabBarController
    window?.makeKeyAndVisible()

    return true
}

您的 View Controller 类中不需要任何特定代码。

JournalTableViewController.swift 片段:

override func viewDidLoad() {
    super.viewDidLoad()
}

关于ios - 以编程方式制作 UITabBarController 和 UINavigationController Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53027275/

相关文章:

iOS psd 到 xcode

ios - 将数据传递给 collectionView(numberOfItemsInSection) 函数 (Swift)

objective-c - 导航栏隐藏不工作 IOS

ios - 如何在 SWIFT 项目中使用 iosMath?响应。如何将 MTMathUILabel() 添加到项目中?

iphone - 从 AppDelegate 获取 Storyboard定义的 UINavigationController

ios - 将子层添加到导航栏与过渡冲突

ios - 如何使用两个单独的 UIBarButtonItems 禁用/启用 UIPanGestureRecognizer?

iphone - 将 UITableViewCell 中的 UITextField 保存到 NSMutableArray

ios - SdWebImage 问题中的事件指示器

arrays - 变量行的索引路径