学习教程,看到这样设计 UITabBarController,感觉很不错。好记性不如烂笔头,记录一下:
let tabBarDelegate = TabBarDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabController = UITabBarController()
let homeStoryboard = UIStoryboard(name: "Home", bundle: nil)
let searchStoryboard = UIStoryboard(name: "Search", bundle: nil)
let newPostStoryboard = UIStoryboard(name: "NewPost", bundle: nil)
let profileStoryboard = UIStoryboard(name: "Profile", bundle: nil)
let activityStoryboard = UIStoryboard(name: "Activity", bundle: nil)
let homeVC = homeStoryboard.instantiateViewController(withIdentifier: "Home") as! HomeViewController
let profileVC = profileStoryboard.instantiateViewController(withIdentifier: "Profile") as! ProfileViewController
let searchVC = searchStoryboard.instantiateViewController(withIdentifier: "Search") as! SearchViewController
let newPostVC = newPostStoryboard.instantiateViewController(withIdentifier: "NewPost") as! NewPostViewController
let activityVC = activityStoryboard.instantiateViewController(withIdentifier: "Activity") as! ActivityViewController
let vcData: [(UIViewController, UIImage, UIImage)] = [
(homeVC, UIImage(named: "home_tab_icon")!, UIImage(named: "home_selected_tab_icon")!),
(searchVC, UIImage(named: "search_tab_icon")!, UIImage(named: "search_selected_tab_icon")!),
(newPostVC, UIImage(named: "post_tab_icon")!, UIImage(named: "post_tab_icon")!),
(activityVC, UIImage(named: "activity_tab_icon")!, UIImage(named: "activity_selected_tab_icon")!),
(profileVC, UIImage(named: "profile_tab_icon")!, UIImage(named: "profile_selected_tab_icon")!)
]
let vcs = vcData.map { (vc, defaultImage, selectedImage) -> UINavigationController in
let nav = UINavigationController(rootViewController: vc)
nav.tabBarItem.image = defaultImage
nav.tabBarItem.selectedImage = selectedImage
return nav
}
tabController.viewControllers = vcs
tabController.tabBar.isTranslucent = false
tabController.delegate = tabBarDelegate
if let items = tabController.tabBar.items {
for item in items {
if let image = item.image {
item.image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
}
if let selectedImage = item.selectedImage {
item.selectedImage = selectedImage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
}
item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
UINavigationBar.appearance().backgroundColor = UIColor.white
window?.rootViewController = tabController
return true
}
TabBarDelegate.swift:
import Foundation
import UIKit
class TabBarDelegate: NSObject, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let navigationController = viewController as? UINavigationController
_ = navigationController?.popViewController(animated: false)
}
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
let selectedViewController = tabBarController.selectedViewController
guard let _selectedViewController = selectedViewController else {
return false
}
if viewController == _selectedViewController {
return false
}
guard let controllerIndex = tabBarController.viewControllers?.index(of: viewController) else {
return true
}
if controllerIndex == 2 {
let newPostStoryboard = UIStoryboard(name: "NewPost", bundle: nil)
let newPostVC = newPostStoryboard.instantiateViewController(withIdentifier: "NewPost") as! NewPostViewController
let navController = UINavigationController(rootViewController: newPostVC)
_selectedViewController.present(navController, animated: true, completion: nil)
return false
}
let navigationController = viewController as? UINavigationController
_ = navigationController?.popToRootViewController(animated: false)
return true
}
}
网友评论