2016-06-09 66 views
0

我正在开发一个应用程序,该应用程序使用了选项卡栏和导航栏。现在,这两个显示在为应用加载的第一页上,但是当我导航到不同的选项卡时,仅显示选项卡栏。导航栏仅显示在第一页iOS Swift

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

这是我的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) 
} 
+0

你为什么要设置UINavigationBar的框架? CustomNavBarController是UINavigationcontroller的子项吗? –

+0

是的,如果不是? –

+0

没有必要为标签栏设置框架。 –

回答

0

我建议你在TabBarController的内部嵌入NavigationController。具体而言,您应该为TabBarController中的每个选项卡插入一个NavigationController。 通过这种方式,您的NavigationBar将在标签和TabBar的导航过程中始终可见。

这里是你的故事板应该如何看起来像一个形象:

enter image description here

干杯!

+0

你有没有关于如何以编程的方式提出任何建议? –

+0

不是真的..故事板是一个非常强大的工具,即使有时它变得有点混乱。它可以让你避免处理大部分时间你忘记的东西。我建议使用它;) –