[英]Push view from navBar in app with tabBar
Im attempting to make an app that has both a UITabBar
(as the root view controller) and also a UINavigationBar
that are both consistently present throughout the app. 我试图制作一个既有
UITabBar
(作为根视图控制器)又有UINavigationBar
的应用程序,这些应用程序在整个应用程序中始终存在。 My issue is that I'm trying to push a viewcontroller from the navBar but I can not find out how to do so. 我的问题是我试图从navBar推送一个viewcontroller,但是我不知道该怎么做。 I understand that I need the
UINavigationBar
to be the NavigationController
of the view controller I'm trying to push from but I cannot figure out how to do this. 我知道我需要
UINavigationBar
成为我要从中推送的视图控制器的NavigationController
,但是我不知道该怎么做。
In appDelegate: 在appDelegate中:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let tabBar = window!.rootViewController
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: window!.frame.width, height: 64))
let navBarItem = UINavigationItem(title: "Title")
navBar.items = [navBarItem]
tabBar?.view.addSubview(navBar)
return true
}
In ViewController im trying to push new VC from 在ViewController中,我试图从中推送新的VC
override func viewDidLoad() {
super.viewDidLoad()
let navBar = self.parentViewController?.view.subviews[2] as! UINavigationBar
let navBarItem = navBar.items[0] as! UINavigationItem
navBarItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Add, target: self, action: "pushCreateVC:")
}
func pushCreateVC(sender: UIBarButtonItem) {
println("push")
let navBar = self.parentViewController?.view.subviews[2] as! UINavigationBar
let navBarItem = navBar.items[0] as! UINavigationItem //left over code to reference the navBar from this method
let createVC = CreateViewController()
self.navigationController?.pushViewController(createVC, animated: true) // This is where I want the navBar to push createVC so it still have the navBar at the top with a back button
}
if you wanna do what you mention in the question, you should create a UITabbarController with UINavgationController(s) in it 如果您想做在问题中提到的事情,则应使用UINavgationController创建一个UITabbarController
// First you need to create a nav controller with viewcontroller in it
var vc = UIViewController()
var navController = UINavigationController(rootViewController: vc)
var tbController = UITabBarController()
//you can add more tab with more navControllers or viewcontrollers
tbController.setViewControllers([navController], animated: true)
//present the tbController as the main viewcontroller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.