iOS 8 Swift navigation bar title, buttons not showing in tab based application(iOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中)
问题描述
我正在尝试遵循 本教程,这个答案 和 this answer 为我的每个标签创建导航栏在 iOS 8/Swift 中基于选项卡的应用程序中,但我的导航栏上没有显示标题或按钮.
I am trying to follow this tutorial, this answer, and this answer to create navigation bars for each of my tabs in a tab-based application in iOS 8 / Swift, but no title or buttons on my navigation bar are showing.
这是我目前所拥有的:
// AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let tabBarController = UITabBarController()
let vc1 = ViewController()
let vc2 = ViewController()
let vc3 = ViewController()
let nc1 = UINavigationController(rootViewController: vc1)
let nc2 = UINavigationController(rootViewController: vc2)
let nc3 = UINavigationController(rootViewController: vc3)
let controllers = [nc1,nc2,nc3]
tabBarController.viewControllers = controllers
nc1.tabBarItem = UITabBarItem(title: "item1", image: nil, tag: 1)
nc2.tabBarItem = UITabBarItem(title: "item2", image: nil, tag: 1)
nc3.tabBarItem = UITabBarItem(title: "item3", image: nil, tag: 1)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
// ViewController.swift
class ViewController: UIViewController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
navigationBar.backgroundColor = UIColor.blueColor()
navigationBar.delegate = self;
let navigationItem = UINavigationItem()
navigationItem.title = "VGl0bGU="
let leftButton = UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
}
func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
return UIBarPosition.TopAttached
}
}
但我只是在模拟器顶部看到一个空白导航栏.
But I am just getting a blank navigation bar on top in the simulator.
推荐答案
您正在制作一个自定义 UINavigationBar
,而 UINavigationController
已经提供给您.
You're making a custom UINavigationBar
when one is already provided to you with the UINavigationController
.
在你的 ViewController
中试试这个:
Try this instead in your ViewController
:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "VGl0bGU="
let navigationBar = navigationController!.navigationBar
navigationBar.tintColor = UIColor.blueColor()
let leftButton = UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
}
这篇关于iOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 8 Swift 导航栏标题,按钮未显示在基于选项卡的应用程序中
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- UITextView 内容插图 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01