在自定义 UITabBarController 中以编程方式导航 (Swift)
Navigate programmatically inside a custom UITabBarController (Swift)
我正在使用基于 Pavel Bogart solution 的自定义 UITabBarController
:非常简单、有用并且非常适合我需要做的事情。
除非有一件事我不知道如何管理:如何以编程方式从 1 VC 切换到另一个(因此,不触摸 tabBar 项目)。
我用 selectedIndex = X
、modalPresentationStyle = .fullScreen
尝试了一些功能,但没有什么真正具体的可以完全按照我们只是触摸 UITabBarController 上的项目来完成。
这是代码:
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupTabBar()
}
var TabControllers = [VC1(), VC2(), VC3(), VC4(), VC5()]
func setupTabBar(){
tabBar.backgroundColor = Theme.navBarBackgroundColor
tabBar.isTranslucent = true
tabBar.clipsToBounds = true
let screen1 = createNavController(vc: TabControllers[0], selected: #imageLiteral(resourceName: "img1on"), unselected: #imageLiteral(resourceName: "img1off"), label: "Name1")
let screen2 = createNavController(vc: TabControllers[1], selected: #imageLiteral(resourceName: "img2on"), unselected: #imageLiteral(resourceName: "img2off"), label: "Name2")
let screen3 = createNavController(vc: TabControllers[2], selected: #imageLiteral(resourceName: "img3on"), unselected: #imageLiteral(resourceName: "img3off"), label: "Name3")
let screen4 = createNavController(vc: TabControllers[3], selected: #imageLiteral(resourceName: "img4on"), unselected: #imageLiteral(resourceName: "img4off"), label: "Name4")
let screen5 = createNavController(vc: TabControllers[4], selected: #imageLiteral(resourceName: "img5on"), unselected: #imageLiteral(resourceName: "img5off"), label: "Name5")
viewControllers = [screen1, screen2, screen3, screen4, screen5]
guard let items = tabBar.items else { return }
for item in items {
let unselectedItem = [NSAttributedString.Key.foregroundColor: Theme.darkTextColor]
let selectedItem = [NSAttributedString.Key.foregroundColor: Theme.mainAccentColor]
let attributes = [NSAttributedString.Key.font: UIFont(name: Theme.subTitle, size: 16)]
item.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .normal)
item.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .selected)
item.setTitleTextAttributes(unselectedItem as [NSAttributedString.Key : Any], for: .normal)
item.setTitleTextAttributes(selectedItem as [NSAttributedString.Key : Any], for: .selected)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppUtility.lockOrientation(.portrait)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppUtility.lockOrientation(.all)
}
}
及其扩展名:
extension UITabBarController {
func createNavController(vc: UIViewController, selected: UIImage, unselected: UIImage, label: String) -> UINavigationController {
let viewController = vc
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = unselected
navController.tabBarItem.selectedImage = selected
navController.tabBarItem.title = label
return navController
}
}
告诉我你的想法。
很奇怪 selectedIndex
对你不起作用,我做了一个类似的解决方案并且对我有用。
尝试正确引用您的根控制器(在本例中为您的 UITabBarController )和子控制器,通过 strong
引用使它们保持活动状态。
我做了一个简单的项目来演示一下。
Programmatic TabBarController repo
希望对您有所帮助!
我正在使用基于 Pavel Bogart solution 的自定义 UITabBarController
:非常简单、有用并且非常适合我需要做的事情。
除非有一件事我不知道如何管理:如何以编程方式从 1 VC 切换到另一个(因此,不触摸 tabBar 项目)。
我用 selectedIndex = X
、modalPresentationStyle = .fullScreen
尝试了一些功能,但没有什么真正具体的可以完全按照我们只是触摸 UITabBarController 上的项目来完成。
这是代码:
class MainTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupTabBar()
}
var TabControllers = [VC1(), VC2(), VC3(), VC4(), VC5()]
func setupTabBar(){
tabBar.backgroundColor = Theme.navBarBackgroundColor
tabBar.isTranslucent = true
tabBar.clipsToBounds = true
let screen1 = createNavController(vc: TabControllers[0], selected: #imageLiteral(resourceName: "img1on"), unselected: #imageLiteral(resourceName: "img1off"), label: "Name1")
let screen2 = createNavController(vc: TabControllers[1], selected: #imageLiteral(resourceName: "img2on"), unselected: #imageLiteral(resourceName: "img2off"), label: "Name2")
let screen3 = createNavController(vc: TabControllers[2], selected: #imageLiteral(resourceName: "img3on"), unselected: #imageLiteral(resourceName: "img3off"), label: "Name3")
let screen4 = createNavController(vc: TabControllers[3], selected: #imageLiteral(resourceName: "img4on"), unselected: #imageLiteral(resourceName: "img4off"), label: "Name4")
let screen5 = createNavController(vc: TabControllers[4], selected: #imageLiteral(resourceName: "img5on"), unselected: #imageLiteral(resourceName: "img5off"), label: "Name5")
viewControllers = [screen1, screen2, screen3, screen4, screen5]
guard let items = tabBar.items else { return }
for item in items {
let unselectedItem = [NSAttributedString.Key.foregroundColor: Theme.darkTextColor]
let selectedItem = [NSAttributedString.Key.foregroundColor: Theme.mainAccentColor]
let attributes = [NSAttributedString.Key.font: UIFont(name: Theme.subTitle, size: 16)]
item.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .normal)
item.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .selected)
item.setTitleTextAttributes(unselectedItem as [NSAttributedString.Key : Any], for: .normal)
item.setTitleTextAttributes(selectedItem as [NSAttributedString.Key : Any], for: .selected)
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
AppUtility.lockOrientation(.portrait)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
AppUtility.lockOrientation(.all)
}
}
及其扩展名:
extension UITabBarController {
func createNavController(vc: UIViewController, selected: UIImage, unselected: UIImage, label: String) -> UINavigationController {
let viewController = vc
let navController = UINavigationController(rootViewController: viewController)
navController.tabBarItem.image = unselected
navController.tabBarItem.selectedImage = selected
navController.tabBarItem.title = label
return navController
}
}
告诉我你的想法。
很奇怪 selectedIndex
对你不起作用,我做了一个类似的解决方案并且对我有用。
尝试正确引用您的根控制器(在本例中为您的 UITabBarController )和子控制器,通过 strong
引用使它们保持活动状态。
我做了一个简单的项目来演示一下。
Programmatic TabBarController repo
希望对您有所帮助!