1, AppDelegate.swift 文件
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,JPUSHRegisterDelegate {
var window: UIWindow?
var myTabarVc=MyTabBarController()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window?.rootViewController = myTabarVc
window?.makeKeyAndVisible()
}
}
2, MyTabBarController.swift 文件
viewDidLoad() {
// 添加子控制器
addChildViewControllers()
}
// 添加子控制器
private func addChildViewControllers() {
setChildViewController(HomeController(), title: "home", imageName: "home")
setChildViewController(MarketViewController(), title: "market", imageName: "market")
setChildViewController(MineViewController(), title: "mine", imageName: "mine")
}
/// 初始化子控制器
private func setChildViewController(_ childController: UIViewController, title: String, imageName: String) {
// 设置 tabbar 文字和图片
childController.tabBarItem.image = UIImage(named: imageName + "_tabbar")
childController.tabBarItem.selectedImage = UIImage(named: imageName + "_tabbar_press")
childController.title = title
// 添加导航控制器为 TabBarController 的子控制器
let navVc = MyNavigationController(rootViewController: childController)
addChildViewController(navVc)
}
3,MyNavigationController.swift 文件
import UIKit
class MyNavigationController: UINavigationController,UINavigationControllerDelegate,UIGestureRecognizerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//导航栏背景颜色
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = UIColor.white
//导航标题文字
UINavigationBar.appearance().titleTextAttributes = [
NSAttributedStringKey.foregroundColor : UIColor.white,
NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19)
]
//添加手势代理
self.interactivePopGestureRecognizer?.delegate = self
self.delegate = self
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
if self.viewControllers.count > 0 {
//添加手势识别
self.interactivePopGestureRecognizer?.isEnabled = true
}
//是否开启动画由传入决定,不会造成冲突
super.pushViewController(viewController, animated: animated)
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
if navigationController.viewControllers.count == 1 {
self.interactivePopGestureRecognizer?.isEnabled = false
}
}
}