【iOS】Swift ESTabBarController的使用


之前也使用Swift編寫過程序,但是由於是多人開發,我沒有從頭搭建,而且之前寫的項目也是多使用oc的庫。我就打算從頭抓包寫一個純Swift的APP。我打算先寫一個簡單的APP,熟練之后在寫比較復雜的APP。具體代碼之后會傳到我的github上面。

在搭建框架的時候發現了這個swift編寫的tabbar控制器。用起來感覺不錯。他可以高度自定義。具體效果可以看官方文檔

但是他還是有一些不足的。這是暫時不支持的功能

/*
 * ESTabBarItem繼承自UITabBarItem,目的是為ESTabBarItemContentView提供UITabBarItem屬性的設置。
 * 目前支持大多常用的屬性,例如image, selectedImage, title, tag 等。
 *
 * Unsupport properties:
 *  MARK: UIBarItem properties
 *      1. var isEnabled: Bool
 *      2. var landscapeImagePhone: UIImage?
 *      3. var imageInsets: UIEdgeInsets
 *      4.  var landscapeImagePhoneInsets: UIEdgeInsets
 *      5. func setTitleTextAttributes(_ attributes: [String : Any]?, for state: UIControlState)
 *      6. func titleTextAttributes(for state: UIControlState) -> [String : Any]?
 *  MARK: UITabBarItem properties
 *      7. var titlePositionAdjustment: UIOffset
 *      8. func setBadgeTextAttributes(_ textAttributes: [String : Any]?, for state: UIControlState)
 *      9. func badgeTextAttributes(for state: UIControlState) -> [String : Any]?
 */

如果你需要上面的功能,就可以停止往下看了。

 

 

 

 

 

 

你既然看到了這里就說明你需要使用這個庫。現把使用方法列出。

1. 系統原生(通過tintColor修改選中顏色)

     let tabBarController = ESTabBarController()
        let v1 = HomeViewController()
        let v2 = HomeViewController()
        let v3 = HomeViewController()
        let v4 = HomeViewController()
        let v5 = HomeViewController()
        
        /// MARK: 使用原生 
        v1.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_home"), selectedImage: UIImage(named: "btn_home_highlight"))
        v2.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_category"), selectedImage: UIImage(named: "btn_category_highlight"))
        v3.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_rank"), selectedImage: UIImage(named: "btn_rank_highlight"))
        v4.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_random"), selectedImage: UIImage(named: "btn_random_highlight"))
        v5.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_favourite"), selectedImage: UIImage(named: "btn_favourite_highlight"))
       
     UITabBar.appearance().tintColor
= UIColor.red tabBarController.tabBar.shadowImage = nil tabBarController.viewControllers = [v1, v2, v3, v4, v5]
     // 不需要導航欄的話 直接 rootviewcontroller =
tabBarController

let navigationController = UINavigationController.init(rootViewController: tabBarController)
    APP_DELEGATE
?.window?.rootViewController = navigationController

2. ESTabBarController仿系統原生(設置tintColor沒反應)

        let tabBarController = ESTabBarController()
        let v1 = HomeViewController()
        let v2 = HomeViewController()
        let v3 = HomeViewController()
        let v4 = HomeViewController()
        let v5 = HomeViewController()
        
        /// MARK: 仿原生 設置 但是不能修改選中顏色 (設置了tintcolor 沒反應)
        v1.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_home"), selectedImage: UIImage(named: "btn_home_highlight"))
        v2.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_category"), selectedImage: UIImage(named: "btn_category_highlight"))
        v3.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_rank"), selectedImage: UIImage(named: "btn_rank_highlight"))
        v4.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_random"), selectedImage: UIImage(named: "btn_random_highlight"))
        v5.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_favourite"), selectedImage: UIImage(named: "btn_favourite_highlight"))
        tabBarController.viewControllers = [v1, v2, v3, v4, v5]
        
        let navigationController = UINavigationController.init(rootViewController: tabBarController)
        APP_DELEGATE?.window?.rootViewController = navigationController

3.系統和ESTabBarController混合使用(設置tintColor只對系統的有效)

      let tabBarController = ESTabBarController()
        let v1 = HomeViewController()
        let v2 = HomeViewController()
        let v3 = HomeViewController()
        let v4 = HomeViewController()
        let v5 = HomeViewController()
        
        /// MARK: 原生和系統混和設置  不常見
        v1.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_home"), selectedImage: UIImage(named: "btn_home_highlight"))
        v2.tabBarItem = UITabBarItem.init(title: "", image: UIImage(named: "btn_category"), selectedImage: UIImage(named: "btn_category_highlight"))
        v3.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_rank"), selectedImage: UIImage(named: "btn_rank_highlight"))
        v4.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_random"), selectedImage: UIImage(named: "btn_random_highlight"))
        v5.tabBarItem = ESTabBarItem.init(title: "", image: UIImage(named: "btn_favourite"), selectedImage: UIImage(named: "btn_favourite_highlight"))
        
        UITabBar.appearance().tintColor = UIColor.red;
        tabBarController.viewControllers = [v1, v2, v3, v4, v5]
        
        let navigationController = UINavigationController.init(rootViewController: tabBarController)
        APP_DELEGATE?.window?.rootViewController = navigationController

4.帶導航欄的Tab設置。

   // 設置導航欄的方法有兩種 一種是像上面的一樣     只設置一次導航欄

// 另一種則是每個控制器自己設置導航欄 
        let tabBarController = ESTabBarController()
        let v1 = HomeViewController()
        let v2 = HomeViewController()
        let v3 = HomeViewController()
        let v4 = HomeViewController()
        let v5 = HomeViewController()
        
        v1.tabBarItem = ESTabBarItem.init(title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
        v3.tabBarItem = ESTabBarItem.init(title: "Photo", image: UIImage(named: "photo"), selectedImage: UIImage(named: "photo_1"))
        v4.tabBarItem = ESTabBarItem.init(title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
        v5.tabBarItem = ESTabBarItem.init(title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
        
        let n1 = UINavigationController.init(rootViewController: v1)
        let n2 = UINavigationController.init(rootViewController: v2)
        let n3 = UINavigationController.init(rootViewController: v3)
        let n4 = UINavigationController.init(rootViewController: v4)
        let n5 = UINavigationController.init(rootViewController: v5)
        
        v1.title = "Home"
        v2.title = "Find"
        v3.title = "Photo"
        v4.title = "List"
        v5.title = "Me"
        
        tabBarController.viewControllers = [n1, n2, n3, n4, n5]
        APP_DELEGATE?.window?.rootViewController = tabBarController                

5.使用自定義選中顏色的ESTabBarController

自定義使用ESTabBarController需要使用這個方法。這個方法是使用自定義屬性的方法。使用它可以設置自定義選中顏色、設置選中的動畫效果、設置選中后的不同效果等。

官方demo中已經有了這樣的效果,我們只需要把我們需要的功能寫個繼承ESTabBarItemContentView的子類或者直接導入所需要的效果

 public init(_ contentView: ESTabBarItemContentView = ESTabBarItemContentView(), title: String? = nil, image: UIImage? = nil, selectedImage: UIImage? = nil, tag: Int = 0)

 

        let tabBarController = ESTabBarController()
        let v1 = ExampleViewController()
        let v2 = ExampleViewController()
        let v3 = ExampleViewController()
        let v4 = ExampleViewController()
        let v5 = ExampleViewController()
        v1.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
        v3.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Photo", image: UIImage(named: "photo"), selectedImage: UIImage(named: "photo_1"))
        v4.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
        v5.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
        
        tabBarController.viewControllers = [v1, v2, v3, v4, v5]
        
        let navigationController = ExampleNavigationController.init(rootViewController: tabBarController)
        tabBarController.title = "Example"
        return navigationController

5.自定義選中效果

使用方法和上面的自定義選中顏色一致。

具體可看:ExampleBasicContentView(選中顏色)

       ExampleBouncesContentView(選中彈簧縮放效果)

               ExampleHighlightableContentView

     ExampleBackgroundContentView(自定義背景)

6.不規則中部大按鈕

導入ExampleIrregularityBasicContentView 或者仿寫這個。

        let tabBarController = ESTabBarController()
        tabBarController.delegate = delegate
        tabBarController.title = "Irregularity"
        tabBarController.tabBar.shadowImage = UIImage(named: "transparent")
        tabBarController.tabBar.backgroundImage = UIImage(named: "background_dark")
        tabBarController.shouldHijackHandler = {
            tabbarController, viewController, index in
            if index == 2 {
                return true
            }
            return false
        }
        tabBarController.didHijackHandler = {
            [weak tabBarController] tabbarController, viewController, index in
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                let alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
                let takePhotoAction = UIAlertAction(title: "Take a photo", style: .default, handler: nil)
                alertController.addAction(takePhotoAction)
                let selectFromAlbumAction = UIAlertAction(title: "Select from album", style: .default, handler: nil)
                alertController.addAction(selectFromAlbumAction)
                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
                alertController.addAction(cancelAction)
                tabBarController?.present(alertController, animated: true, completion: nil)
            }
        }
        
        let v1 = ExampleViewController()
        let v2 = ExampleViewController()
        let v3 = ExampleViewController()
        let v4 = ExampleViewController()
        let v5 = ExampleViewController()
        
        v1.tabBarItem = ESTabBarItem.init(ExampleIrregularityBasicContentView(), title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(ExampleIrregularityBasicContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
        v3.tabBarItem = ESTabBarItem.init(ExampleIrregularityContentView(), title: nil, image: UIImage(named: "photo_verybig"), selectedImage: UIImage(named: "photo_verybig"))
        v4.tabBarItem = ESTabBarItem.init(ExampleIrregularityBasicContentView(), title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
        v5.tabBarItem = ESTabBarItem.init(ExampleIrregularityBasicContentView(), title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
        
        tabBarController.viewControllers = [v1, v2, v3, v4, v5]
        
        let navigationController = ExampleNavigationController.init(rootViewController: tabBarController)
        tabBarController.title = "Example"
        APP_DELEGATE?.window?.rootViewController = tabBarController

7.使用Lottie

在開發中我們也會遇到一些動畫效果APP不好實現,但是UI可以輕松實現的情況。這時候我們可以使用lottie來實現這個效果。ESTabBarController也支持使用Lottie。只需要到入ExampleLottieAnimateBasicContentView就可以了。

 

demo里面還有其他例子(像tabbar上有more(原生、ESTabBarController、混合使用),通知,添加自定義提醒框等)。不常見我也就不碼了。有這樣的需求可以自己看代碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM