2016-06-09 63 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

不是真的..故事板是一個非常強大的工具,即使有時它變得有點混亂。它可以讓你避免處理大部分時間你忘記的東西。我建議使用它;) –