swift项目中,tabbarviewcontroller文件继承uitabbarcontroller,添加一个uinavigantioncontroller,实现点击不同的tab可以切换图片与文字颜色,且跳转到不同的view,现在有5个viewcontroller,分别是X、Y、Z、M、N
时间: 2023-05-20 14:02:45 浏览: 149
你可以在tabbarviewcontroller的viewDidLoad方法中添加以下代码实现:
```
// 创建5个view controller
let xVC = XViewController()
let yVC = YViewController()
let zVC = ZViewController()
let mVC = MViewController()
let nVC = NViewController()
// 设置每个view controller的tabBarItem
xVC.tabBarItem = UITabBarItem(title: "X", image: UIImage(named: "x_unselected"), selectedImage: UIImage(named: "x_selected"))
yVC.tabBarItem = UITabBarItem(title: "Y", image: UIImage(named: "y_unselected"), selectedImage: UIImage(named: "y_selected"))
zVC.tabBarItem = UITabBarItem(title: "Z", image: UIImage(named: "z_unselected"), selectedImage: UIImage(named: "z_selected"))
mVC.tabBarItem = UITabBarItem(title: "M", image: UIImage(named: "m_unselected"), selectedImage: UIImage(named: "m_selected"))
nVC.tabBarItem = UITabBarItem(title: "N", image: UIImage(named: "n_unselected"), selectedImage: UIImage(named: "n_selected"))
// 创建一个数组来存储这5个view controller
let viewControllers = [xVC, yVC, zVC, mVC, nVC]
// 将这个数组设置为tabBarController的viewControllers
self.viewControllers = viewControllers
// 设置tabBar的tintColor
tabBar.tintColor = UIColor.red
```
这样就可以实现点击不同的tab可以切换图片与文字颜色,且跳转到不同的view。
阅读全文