Swift ESTabBarController 大中心按钮失败 [英] Swift ESTabBarController large center button fail

查看:44
本文介绍了Swift ESTabBarController 大中心按钮失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载并运行

虽然下图来自示例项目,但也符合我的期望这是我的代码

AppDelegate.swift

let TabbarNavigationController = ViewController.customIrregularityStyle(delegate: nil)self.window?.rootViewController = TabbarNavigationController

ViewController.swift

static func customIrregularityStyle(delegate: UITabBarControllerDelegate?) ->导航控制器{让 tabBarController = ESTabBarController()tabBarController.delegate = 委托tabBarController.title = "不规则"tabBarController.tabBar.shadowImage = UIImage(named: "transparent")tabBarController.tabBar.backgroundImage = UIImage(named: "background_dark")tabBarController.shouldHijackHandler = {tabbarController, viewController, index in如果索引 == 2 {返回真}返回假}tabBarController.didHijackHandler = {[弱tabBarController] tabbarController, viewController, index inDispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {让 alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)let takePhotoAction = UIAlertAction(title: "拍照", style: .default, handler: nil)alertController.addAction(takePhotoAction)让 selectFromAlbumAction = UIAlertAction(title: "从专辑中选择", 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)}}让 v1 = ExampleViewController()让 v2 ​​= ExampleViewController()让 v3 = ExampleViewController()让 v4 = ExampleViewController()让 v5 = ExampleViewController()v1.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))v2.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))v3.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: nil, image: UIImage(named: "photo_verybig"), selectedImage: UIImage(named: "photo_verybig"))v4.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))v5.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))tabBarController.viewControllers = [v1, v2, v3, v4, v5]让 navigationController = NavigationController.init(rootViewController: tabBarController)tabBarController.title = "示例"返回导航控制器}

有什么想法吗?

解决方案

尝试使用自定义 tabBar 并在 tabBar 中心添加按钮

I download and run ESTabBarController fine but I don't know why when I create my own project and use code from Example, my large button still small??

While image below is from example project as my expectation as well Here is my code

AppDelegate.swift

let TabbarNavigationController = ViewController.customIrregularityStyle(delegate: nil)
self.window?.rootViewController = TabbarNavigationController    

ViewController.swift

static func customIrregularityStyle(delegate: UITabBarControllerDelegate?) -> NavigationController {
    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(IrregularityBasicContentView(), title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
    v2.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
    v3.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: nil, image: UIImage(named: "photo_verybig"), selectedImage: UIImage(named: "photo_verybig"))
    v4.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
    v5.tabBarItem = ESTabBarItem.init(IrregularityBasicContentView(), title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))

    tabBarController.viewControllers = [v1, v2, v3, v4, v5]

    let navigationController = NavigationController.init(rootViewController: tabBarController)
    tabBarController.title = "Example"
    return navigationController
}

Any idea?

解决方案

Try to use custom tabBar and add your button in centre of tabBar Like This

If you want to use ESTabBarController follow the below steps.

Step 1: Download the sample project of ESTabBarController and install pod "ESTabBarController-swift" in your project

Step 2: Copy all the assets from sample project into your existing project

Step 3: Copy ExampleBasicContentView, ExampleBouncesContentView , ExampleIrregularityContentView, ExampleNavigationController Classes from sample project

NOTE - If you got error "No such module 'pop'" in "ExampleIrregularityBasicContentView" Then remove that line and remove func playMaskAnimation Or install pod pop

Step 4: In StoryBoard Embbed your viewController in navigationController and assign ExampleNavigationController class to your navigationController

Step 5: import ESTabBarController_swift and add UITabBarControllerDelegate in your AppDelegate class Add paste following code in your AppDelegate class

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


        let tabBarController = ESTabBarController()
        tabBarController.delegate = self
        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 = ViewController()
        let v2 = ViewController()
        let v3 = ViewController()
        let v4 = ViewController()
        let v5 = ViewController()

        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)
        self.window?.rootViewController = navigationController

         return true
    }

You can find demo here

ScreenShot :

这篇关于Swift ESTabBarController 大中心按钮失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆