我正在开发一个同时使用Tab Bar和Nav Bar的应用程序。现在,这两个都显示在为应用程序加载的第一页上,但是当我导航到不同的选项卡时,只显示标签栏。
我的问题是我并不完全确定导航栏实际被告知要显示的位置。我看到了关于必须将导航栏和标签栏绑在一起的帖子,但我并没有完全理解它们,并且尝试实现它们导致我的应用程序根本无法加载。我应该在每个视图控制器中实例化导航栏吗?
这是我的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)
}