UINavigationController 和 TabBarController 软件 /没有加热器/

目前我的文件 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

s 正确调整它的方法是做
UITabBarController


rootViewController

. 每个标签都是你的
UITabBarController

得到你自己的
UINavigationController

. 所以,如果你有 4 您创建的标签 4
UINavigationControllers

.

请参阅部分:
https://developer.apple.com/li ... .html

更新

根据您添加到更新的问题的代码,创建
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

s 安装
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
}
}


就个人而言,我喜欢这种方法,因为他从一切中拯救了我的重复代码 ViewControllers 并保存导航条码 tab & 在您的用户类中组织 tab bar.

如果您不想在整个应用程序中使用相同的选项卡或导航面板设置,则可以在各自的类别中单独配置它们 ViewControllers. 但是,如果每个选项卡都有不同的属性 tab 和 / 或者 nav, 回答 Vacawama, 最有可能,这将是最好的方法!

风见雨下

赞同来自:

你可能想试试它:


viewControllers = [vc1, vc2, vc3].map{UINavigationController/rootViewController: $0/}

要回复问题请先登录注册