gpt4 book ai didi

ios - 导航栏仅显示在第一页 iOS Swift

转载 作者:行者123 更新时间:2023-11-30 13:21:31 25 4
gpt4 key购买 nike

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

我的问题是,我不完全确定导航栏实际上被告知显示在哪里。我看到有关必须将导航栏和选项卡栏捆绑在一起的帖子,但我没有完全理解它们,并且尝试实现它们导致我的应用程序根本无法加载。我应该在每个 View Controller 中实例化导航栏吗?

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

最佳答案

我建议您在TabBarController中嵌入一个NavigationController。具体来说,您应该为 TabBarController 中的每个选项卡插入一个 NavigationController。这样,在选项卡和 TabBar 内导航期间,您的 NavigationBar 将始终可见。

这是您的 Storyboard的外观图片:

enter image description here

干杯!

关于ios - 导航栏仅显示在第一页 iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37731321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com