Swift TabBarController,UINavigationController 設(shè)置 (包含開啟側(cè)滑手勢(shì)返回)

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) {
        // 設(shè)置 tabbar 文字和圖片
        childController.tabBarItem.image = UIImage(named: imageName + "_tabbar")
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_tabbar_press")
        
        childController.title = title
        // 添加導(dǎo)航控制器為 TabBarController 的子控制器
        let navVc = MyNavigationController(rootViewController: childController)
        
        addChildViewController(navVc)
    }

3,MyNavigationController.swift 文件


import UIKit
class MyNavigationController: UINavigationController,UINavigationControllerDelegate,UIGestureRecognizerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //導(dǎo)航欄背景顏色
        UINavigationBar.appearance().isTranslucent = false
        UINavigationBar.appearance().barTintColor =  UIColor.white
        
        //導(dǎo)航標(biāo)題文字
        UINavigationBar.appearance().titleTextAttributes = [
            NSAttributedStringKey.foregroundColor : UIColor.white,
            NSAttributedStringKey.font : UIFont.systemFont(ofSize: 19)
        ]
        
        //添加手勢(shì)代理
        self.interactivePopGestureRecognizer?.delegate = self
        self.delegate = self
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.viewControllers.count > 0 {
            //添加手勢(shì)識(shí)別
            self.interactivePopGestureRecognizer?.isEnabled = true
        }
         //是否開啟動(dòng)畫由傳入決定,不會(huì)造成沖突
        super.pushViewController(viewController, animated: animated)
    }
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if navigationController.viewControllers.count == 1 {
            self.interactivePopGestureRecognizer?.isEnabled = false
        }
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容