Ios Swift : 在自定义标签栏中显示导航栏
Ios Swift : Displaying Navigation Bar in custom Tabbar
我正在尝试使用以下代码以编程方式创建自定义选项卡栏以将其放置在屏幕顶部。但问题是,我无法在标签栏顶部获得导航栏以返回上一屏幕。
请帮助我。
override func viewDidLoad()
{
super.viewDidLoad()
let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
let TabBarCtrVar = UITabBarController()
let TabBarVar = TabBarCtrVar.tabBar
let ObxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("ObxSrnUid")
let ObxNavSrnVar = UINavigationController(rootViewController: ObxSrnVar)
let IbxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("IbxSrnUid")
let IbxNavSrnVar = UINavigationController(rootViewController: IbxSrnVar)
UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar
ObxSrnVar.tabBarItem = UITabBarItem(
title: "Outbox",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
IbxSrnVar.tabBarItem = UITabBarItem(
title: "Inbox",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
TabBarCtrVar.viewControllers = [ObxSrnVar,IbxSrnVar]
TabBarVar.tintColor = UIColor.greenColor()
TabBarVar.frame = CGRectMake(
0,
UIApplication.sharedApplication().statusBarFrame.size.height + 64,
UIScreen.mainScreen().bounds.width,
50)
}
这对我有用。希望对你也有用
let myStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let myTabBarController = UITabBarController()
let leftController = myStoryBoard.instantiateViewControllerWithIdentifier("left")
let leftNavController = UINavigationController(rootViewController: leftController)
let rightController = myStoryBoard.instantiateViewControllerWithIdentifier("right")
let rightNavController = UINavigationController(rootViewController: rightController)
UIApplication.sharedApplication().delegate!.window?!.rootViewController = myTabBarController
myTabBarController.addChildViewController(leftNavController)
myTabBarController.addChildViewController(rightNavController)
let leftChild = myStoryBoard.instantiateViewControllerWithIdentifier("leftChild")
let rightChild = myStoryBoard.instantiateViewControllerWithIdentifier("rightChild")
leftNavController.addChildViewController(leftChild)
rightNavController.addChildViewController(rightChild)
print(myTabBarController.viewControllers?.count)
let but1 = myTabBarController.viewControllers?[0].tabBarItem
but1!.title = "Left Page"
let but2 = myTabBarController.viewControllers?[1].tabBarItem
but2!.title = "Right Page"
leftNavController.childViewControllers[0].navigationItem.title = "Left View"
rightNavController.childViewControllers[0].navigationItem.title = "Right View"
下面的代码让我可以获取导航栏。但是我无法添加后退按钮并使其能够移动到上一个屏幕。
谁能帮我解决这个问题。
let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
let TabBarCtrVar = UITabBarController()
let TabBarVar = TabBarCtrVar.tabBar
UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar
let Tab1SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab1SrnXid")
let Tab2SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab2SrnXid")
let Tab1NavCtrVar = UINavigationController(rootViewController: Tab1SrnVar)
let Tab2NavCtrVar = UINavigationController(rootViewController: Tab2SrnVar)
Tab1NavCtrVar.childViewControllers[0].navigationItem.title = "Nam1TabSrn"
Tab2NavCtrVar.childViewControllers[0].navigationItem.title = "Nam2TabSrn"
Tab1NavCtrVar.tabBarItem = UITabBarItem(
title: "Nam1Tab",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
Tab2NavCtrVar.tabBarItem = UITabBarItem(
title: "Nam2Tab",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
TabBarCtrVar.viewControllers = [Tab1NavCtrVar,Tab2NavCtrVar]
TabBarVar.tintColor = UIColor.greenColor()
TabBarVar.frame = CGRectMake(
0,
UIApplication.sharedApplication().statusBarFrame.size.height + 40,
UIScreen.mainScreen().bounds.width,
50)
我正在尝试使用以下代码以编程方式创建自定义选项卡栏以将其放置在屏幕顶部。但问题是,我无法在标签栏顶部获得导航栏以返回上一屏幕。 请帮助我。
override func viewDidLoad()
{
super.viewDidLoad()
let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
let TabBarCtrVar = UITabBarController()
let TabBarVar = TabBarCtrVar.tabBar
let ObxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("ObxSrnUid")
let ObxNavSrnVar = UINavigationController(rootViewController: ObxSrnVar)
let IbxSrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("IbxSrnUid")
let IbxNavSrnVar = UINavigationController(rootViewController: IbxSrnVar)
UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar
ObxSrnVar.tabBarItem = UITabBarItem(
title: "Outbox",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
IbxSrnVar.tabBarItem = UITabBarItem(
title: "Inbox",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
TabBarCtrVar.viewControllers = [ObxSrnVar,IbxSrnVar]
TabBarVar.tintColor = UIColor.greenColor()
TabBarVar.frame = CGRectMake(
0,
UIApplication.sharedApplication().statusBarFrame.size.height + 64,
UIScreen.mainScreen().bounds.width,
50)
}
这对我有用。希望对你也有用
let myStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let myTabBarController = UITabBarController()
let leftController = myStoryBoard.instantiateViewControllerWithIdentifier("left")
let leftNavController = UINavigationController(rootViewController: leftController)
let rightController = myStoryBoard.instantiateViewControllerWithIdentifier("right")
let rightNavController = UINavigationController(rootViewController: rightController)
UIApplication.sharedApplication().delegate!.window?!.rootViewController = myTabBarController
myTabBarController.addChildViewController(leftNavController)
myTabBarController.addChildViewController(rightNavController)
let leftChild = myStoryBoard.instantiateViewControllerWithIdentifier("leftChild")
let rightChild = myStoryBoard.instantiateViewControllerWithIdentifier("rightChild")
leftNavController.addChildViewController(leftChild)
rightNavController.addChildViewController(rightChild)
print(myTabBarController.viewControllers?.count)
let but1 = myTabBarController.viewControllers?[0].tabBarItem
but1!.title = "Left Page"
let but2 = myTabBarController.viewControllers?[1].tabBarItem
but2!.title = "Right Page"
leftNavController.childViewControllers[0].navigationItem.title = "Left View"
rightNavController.childViewControllers[0].navigationItem.title = "Right View"
下面的代码让我可以获取导航栏。但是我无法添加后退按钮并使其能够移动到上一个屏幕。 谁能帮我解决这个问题。
let SrnBrdVar = UIStoryboard(name: "Main", bundle: nil)
let TabBarCtrVar = UITabBarController()
let TabBarVar = TabBarCtrVar.tabBar
UIApplication.sharedApplication().delegate!.window?!.rootViewController = TabBarCtrVar
let Tab1SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab1SrnXid")
let Tab2SrnVar = SrnBrdVar.instantiateViewControllerWithIdentifier("Tab2SrnXid")
let Tab1NavCtrVar = UINavigationController(rootViewController: Tab1SrnVar)
let Tab2NavCtrVar = UINavigationController(rootViewController: Tab2SrnVar)
Tab1NavCtrVar.childViewControllers[0].navigationItem.title = "Nam1TabSrn"
Tab2NavCtrVar.childViewControllers[0].navigationItem.title = "Nam2TabSrn"
Tab1NavCtrVar.tabBarItem = UITabBarItem(
title: "Nam1Tab",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
Tab2NavCtrVar.tabBarItem = UITabBarItem(
title: "Nam2Tab",
image: UIImage(named: "NamItmDftImj"),
selectedImage: UIImage(named: "NamItmSltImj")?.imageWithRenderingMode(.AlwaysOriginal))
TabBarCtrVar.viewControllers = [Tab1NavCtrVar,Tab2NavCtrVar]
TabBarVar.tintColor = UIColor.greenColor()
TabBarVar.frame = CGRectMake(
0,
UIApplication.sharedApplication().statusBarFrame.size.height + 40,
UIScreen.mainScreen().bounds.width,
50)