【发布时间】:2015-10-04 20:10:41
【问题描述】:
我正在尝试关注 this tutorial、this answer 和 this answer 在 iOS 8 / Swift 中基于选项卡的应用程序中为我的每个选项卡创建导航栏,但导航栏上没有标题或按钮正在显示。
这是我目前所拥有的:
// 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 = "Title"
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
}
}
但我只是在模拟器顶部看到一个空白导航栏。
【问题讨论】:
-
标题你试过了吗
self.title = "Your Title" -
看起来我的导航栏部分位于状态栏下方。当我使用尺寸时,我可以看到标题和按钮伸出底部。
-
阿拉丁,你走对了。我正在创建两个导航栏。导航控制器带有导航栏,我需要使用 self.navigationItem.title = "My Title" 设置标题。
-
这就是我无法理解的,我想知道您为什么要创建一个新的导航栏,而您的视图控制器已经嵌入到一个已经有栏的导航控制器中
标签: ios iphone swift uinavigationcontroller uitabbarcontroller