xcode 在基于标签栏的应用程序上添加导航栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34085415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Navigation Bar on Tab-Bar Based App
提问by senty
I have a tab-bar based app and I want to add Navigation Bar at the top of the app. Please note that I am using a library called PageMenuthat creates 2 TableViews inside 1 parent ViewController.
我有一个基于标签栏的应用程序,我想在应用程序的顶部添加导航栏。请注意,我正在使用一个名为PageMenu的库,它在 1 个父 ViewController 中创建了 2 个 TableView。
What I tried was, adding a new ViewController and Editor->Embed in Navigation Bar. Place it before Tab Bar Controller, ctrl+drag to Tab Bar Controller to set the relationship of root view. Finally set Nav Bar Controller as initial view controller. But this fails like this:
我尝试的是,添加一个新的 ViewController 和 Editor->Embed in Navigation Bar。放在Tab Bar Controller之前,ctrl+drag到Tab Bar Controller设置根视图的关系。最后将 Nav Bar Controller 设置为初始视图控制器。但这失败了:
(Top became pretty weird, blurry and the sub-header of PageMenu got disappeared. Maybe it's under that blurry thing because I can still swipe between 2 table views.
(顶部变得非常奇怪,模糊,PageMenu 的子标题消失了。也许它在那个模糊的东西下面,因为我仍然可以在 2 个表视图之间滑动。
Secondly, I tried removing the Navigation Controller, and add Navigation Bar to the ViewControllers manually. This worked for table view and view controllers but not the PageMenu one. When I tried it on PageMenu Controller, it didn't show any navigation bar.
其次,我尝试删除导航控制器,并手动将导航栏添加到视图控制器。这适用于表视图和视图控制器,但不适用于 PageMenu 控制器。当我在 PageMenu Controller 上尝试时,它没有显示任何导航栏。
Please note that, in the Demo, they used Navigation Bar as Parent and sub-TableViews, and they achieved Navigation Bar with this as well as Storyboard > Navigation Controller:
请注意,在 Demo 中,他们使用 Navigation Bar 作为 Parent 和 sub-TableViews,他们用这个以及 Storyboard > Navigation Controller 实现了 Navigation Bar:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "HEADER"
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
Lastly, I tried..
最后,我试了..
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav1 = UINavigationController()
let first = ViewController(nibName: nil, bundle: nil)
nav1.viewControllers = [first]
let second = SecondViewController(nibName: "SecondViewController", bundle: nil)
let nav2 = UINavigationController()
nav2.viewControllers = [second]
let tabs = UITabBarController()
tabs.viewControllers = [nav1, nav2]
self.window!.rootViewController = tabs;
self.window?.makeKeyAndVisible();
return true
}
But the result I get is:
但我得到的结果是:
What I want to achieve (but with TabBarController; NavBar just for header):
我想要实现的目标(但使用 TabBarController;NavBar 仅用于标题):
What I have now is this. I just want to add NavigationBar at the top of it like the above PageMenu example
我现在拥有的是这个。我只想像上面的 PageMenu 示例一样在它的顶部添加 NavigationBar
回答by Nimit Parekh
You can also create like wise story board that helps to solve your problem.
您还可以创建类似明智的故事板来帮助解决您的问题。
Here I can created sample code what you want no single line code change but changes into storyboard only.
在这里,我可以创建示例代码,您不希望更改单行代码,只更改故事板。
回答by Rushang Prajapati
Not add Editor->Embed in Navigation Bar before Tab Bar Controller add Editor->Embed in Navigation Bar before of View controller which you connect from Tab Bar controller.
不要在 Tab Bar Controller 之前添加 Editor->Embed in Navigation Bar 在从 Tab Bar 控制器连接的 View 控制器之前添加 Editor->Embed in Navigation Bar。
Because its work for particular Tab Vise so we have to add Editor->Embed in Navigation Bar before of View controller
因为它适用于特定的 Tab Vise 所以我们必须在 View controller 之前添加 Editor->Embed in Navigation Bar