[英]How to transition from viewcontroller to TabbarController Programmatically without StoryBoards?
[英]UINavigationController, and TabBarController programmatically (no storyboards)
目前,我的 AppDelegate 文件包含以下代码,用于将 CustomTabBarController 建立为 rootViewController:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = CustomTabBarController()
我希望我的应用程序始终在底部有 CustomTabBarController,但我希望每个选项卡都有一个导航控制器。 这是我用来设置我的 tabBarController 的代码:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = FirstViewController()
let vc2 = SecondViewController()
let vc3 = ThirdViewController()
viewControllers = [vc1, vc2, vc3]
}
这是我用来设置 FirstViewController 的代码:
class ProfileViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VCCellId, for: indexPath) as! firstVCCell
return cell
}
}
当组合UITabBarController
和UINavigationController
,正确的设置方法是让UITabBarController
成为rootViewController
。 您的UITabBarController
每个选项卡都有自己的UINavigationController
。 因此,如果您有 4 个选项卡,您将创建 4 个UINavigationControllers
。
请参阅: 将导航控制器添加到选项卡栏界面
更新
你在你的更新问题添加的代码的建立了,创造UINavigationController
为每个的vc1
, vc2
和vc3
。
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = UINavigationController(rootViewController: FirstViewController())
let vc2 = UINavigationController(rootViewController: SecondViewController())
let vc3 = UINavigationController(rootViewController: ThirdViewController())
viewControllers = [vc1, vc2, vc3]
}
}
在您的每个ViewController
,将title
设置为您希望在选择该选项卡时在导航栏中显示的标题:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
title = "First"
self.navigationController?.navigationBar.titleTextAttributes =
[NSFontAttributeName: UIFont(name: "Chalkduster", size: 27)!,
NSForegroundColorAttributeName: UIColor.black]
}
}
我在接受的答案的评论中注意到您想知道在哪里更改导航栏的属性。 如果您计划对每个选项卡应用相同的自定义,您可以在 CustomTabBarController 中使用以下代码:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Tab Bar Customisation
tabBar.barTintColor = .systemPink
tabBar.tintColor = .systemTeal
tabBar.unselectedItemTintColor = .systemGray
tabBar.isTranslucent = false
viewControllers = [
createTabBarItem(tabBarTitle: "Tab 1", tabBarImage: "TabBarImg1", viewController: ViewControllerOne()),
createTabBarItem(tabBarTitle: "Tab 2", tabBarImage: "TabBarImg2", viewController: ViewControllerTwo()),
createTabBarItem(tabBarTitle: "Tab 3", tabBarImage: "TabBarImg3", viewController: ViewControllerThree()),
createTabBarItem(tabBarTitle: "Tab 4", tabBarImage: "TabBarImg4", viewController: ViewControllerFour())
]
}
func createTabBarItem(tabBarTitle: String, tabBarImage: String, viewController: UIViewController) -> UINavigationController {
let navCont = UINavigationController(rootViewController: viewController)
navCont.tabBarItem.title = tabBarTitle
navCont.tabBarItem.image = UIImage(named: tabBarImage)
// Nav Bar Customisation
navCont.navigationBar.barTintColor = .systemRed
navCont.navigationBar.tintColor = .systemBlue
navCont.navigationBar.isTranslucent = false
return navCont
}
}
就我个人而言,我喜欢这种方法,因为它使我免于在整个 ViewController 中重复代码,并在您的自定义标签栏类中组织标签和导航栏代码。
如果您不想在整个应用程序中使用相同的选项卡或导航栏自定义,您可以简单地在各自的 ViewControllers 类中单独自定义每个选项卡或导航栏。 但是,如果每个标签都有不同的标签和/或导航属性,那么 Vacawama 的答案很可能是最好的方法!
你可能想试试这个:
viewControllers = [vc1, vc2, vc3].map{UINavigationController(rootViewController: $0)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.