zoukankan      html  css  js  c++  java
  • 【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、混合使用),通知,添加自定义提醒框等)。不常见我也就不码了。有这样的需求可以自己看代码。

  • 相关阅读:
    onTouchEvent的参数详解
    test
    List泛型自定义排序
    unity3d camera.culling mask
    云计算实验一
    软件设计——职责链模式之审批处理
    sqlserver配置sa用户
    软件设计——模板方法模式之对数据库的操作
    flask入门
    课程总结+申请加分
  • 原文地址:https://www.cnblogs.com/weicyNo-1/p/10184751.html
Copyright © 2011-2022 走看看