【问题标题】:Nav Bar Only Displaying On First Page iOS Swift导航栏仅显示在第一页 iOS Swift
【发布时间】:2016-06-10 00:51:10
【问题描述】:

我正在开发一个同时使用标签栏和导航栏的应用。现在,这两个都显示在为应用加载的第一页上,但当我导航到不同的选项卡时,只显示选项卡栏。

我的问题是我不完全确定导航栏实际被告知显示的位置。我看到有关必须将导航栏和标签栏绑定在一起的帖子,但我并没有完全理解它们,并且尝试实现它们导致我的应用程序根本无法加载。我应该在每个视图控制器中实例化导航栏吗?

这是我的 AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Initialize the window
    window = UIWindow.init(frame: UIScreen.mainScreen().bounds)

    // Set Background Color of window
    window?.backgroundColor = UIColor.whiteColor()

    // Make the window visible
    window!.makeKeyAndVisible()

    // Create TabBarController
    let tabBarController = CustomTabBarController()
    window?.rootViewController = tabBarController

    return true
}

这是我的 CustomTabBarController.swift:

override func viewWillAppear(animated: Bool) {

    // color TabBar correctly
    let darkTeal = UIColor(red:0.09, green:0.62, blue:0.56, alpha:1.0)
    let lightTeal = UIColor(red:0.6, green:0.78, blue:0.74, alpha:1.0)
    UITabBar.appearance().barTintColor = darkTeal
    UITabBar.appearance().tintColor = UIColor.whiteColor()
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Normal)

    // customize TabBarItem width
    let tabBarItemWidth = UIScreen.mainScreen().bounds.width / CGFloat(4)
    UITabBar.appearance().selectionIndicatorImage =
        UIImage().makeImageWithColorAndSize(lightTeal, size: CGSize(width: tabBarItemWidth, height: 49.0))

    // create Tab Bar items
    let findOutVC = FindOut()
    let goOutVC = GoOut()
    let speakOutVC = SpeakOut()
    let reachOutVC = ReachOut()

    // images
    let findout = UIImage(named: "find_out")
    let goout = UIImage(named: "go_out")
    let speakout = UIImage(named: "speak_out")
    let reachout = UIImage(named: "reach_out")

    // modify tabBar items
    findOutVC.tabBarItem = UITabBarItem(
        title: "Find Out",
        image: findout,
        tag: 1)
    goOutVC.tabBarItem = UITabBarItem(
        title: "Go Out",
        image: goout,
        tag: 2)
    speakOutVC.tabBarItem = UITabBarItem(
        title: "Speak Out",
        image: speakout,
        tag: 3)
    reachOutVC.tabBarItem = UITabBarItem(
        title: "Reach Out",
        image: reachout,
        tag: 4)

    // set up tabBar items
    let tabs = [findOutVC, goOutVC, speakOutVC, reachOutVC]
    self.viewControllers = tabs

}

这是我的 CustomNavBarController。

override func viewDidAppear(animated: Bool) {
    // Do any additional setup after loading the view.
    let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44))

    // change color of nav bar
    let lightTeal = UIColor(red:0.6, green:0.78, blue:0.74, alpha:1.0)
    navigationBar.barTintColor = lightTeal
    navigationBar.translucent = true
    navigationBar.delegate = self

    let navigationItem = UINavigationItem()
    navigationItem.title = "shOUT"

    // left button
    let leftButton = UIBarButtonItem(title: "Info", style: UIBarButtonItemStyle.Done, target: self, action: "openInfo")
    let info = UIImage(named: "info")
    leftButton.image = info
    navigationItem.leftBarButtonItem = leftButton

    // right button
    let rightButton = UIBarButtonItem(title: "Pencil", style: UIBarButtonItemStyle.Done, target: self, action: "openWrite")
    let pencil = UIImage(named: "pencil")
    rightButton.image = pencil
    navigationItem.rightBarButtonItem = rightButton

    navigationBar.barStyle = UIBarStyle.Black

    navigationBar.items = [navigationItem]
    self.view.addSubview(navigationBar)
}

【问题讨论】:

  • 为什么要设置 UINavigationBar 的框架? CustomNavBarController 是 UINavigationcontroller 的子级吗?
  • 是的,不是吗?
  • tabbar不用设置frame了。

标签: ios swift uinavigationcontroller uinavigationbar


【解决方案1】:

我建议您在TabBarController 中嵌入NavigationController。具体来说,您应该为TabBarController 中的每个选项卡插入一个NavigationController。 通过这种方式,您的NavigationBar 将在您在选项卡内导航期间始终可见,TabBar 也是如此。

这是您的故事板应该是什么样子的图像:

干杯!

【讨论】:

  • 您对如何以编程方式执行此操作有什么建议吗?
  • 不是真的......故事板是一个非常强大的工具,即使有时它会变得有点混乱。它可以让你避免处理很多你大部分时间忘记的东西。我建议使用它;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
  • 1970-01-01
相关资源
最近更新 更多