【问题标题】:how use navigation controller with xib in Swift如何在 Swift 中使用带有 xib 的导航控制器
【发布时间】:2019-11-03 10:53:20
【问题描述】:

我有一个非常有趣的问题,碰巧我正在学习 Swift,在我看来,xcode 提供的工具让工作变得更容易了,但至少基于我在 Android 项目中的经验建议使用 MVC 来维护项目的秩序,同样尝试 swift 他们建议我使用 xib 文件,它也可以用于工作团体。直到一切都很棒,但是当我尝试实现它时,我发现它太复杂了,为此我更喜欢使用 storyboards 但是库和其他文件的使用使得在加载我的故事板文件时我也延迟了很多是因为它加载了足够多的ViewController。除此之外,在故事板中使用 NavigationController 很容易,它使导航更容易。我的问题是我怎么能用 xib 文件做到这一点?稍后我尝试这样做,但我的 ViewController 垂直加载并且没有导航栏,我不知道如何开发它。

通过代码加载一个ViewController是这样的。

let Storyboard = UIStoryboard(name: "Main", bundle: nil)
        let menuVC = Storyboard.instantiateViewController(withIdentifier: "MenuSelectedViewController") as! MenuSelectedViewController
        self.navigationController?.pushViewController(menuVC, animated: true)

通过界面生成器与 segue

那么如何使用 xib 文件和我的 TabBarController 开发这个 导航栏

【问题讨论】:

    标签: swift model-view-controller uinavigationcontroller uitabbarcontroller xib


    【解决方案1】:

    你只需要UINavigationController(rootViewController: yourViewController)

    这是一个使用这个的例子

    let yourViewController = DiscoverViewController(nibName: "yourViewController", bundle: nil)
    yourViewController.tabBarItem.image = UIImage(named: "imageName")
    let navigationController = UINavigationController(rootViewController: yourViewController)
    
    // TabBarController
    let tabbarController = UITabBarController()
    tabbarController.tabBar.tintColor = ThemeColor
    tabbarController.tabBar.barTintColor = .white
    tabbarController.viewControllers = [navigationController] //add your other  controllers here as needed
    
    // Make it root or what ever you want here
    self.window?.rootViewController = tabbarController
    self.window?.makeKeyAndVisible()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 2016-11-02
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多