如何在 swift 3.0 中使用自定义 UI 创建标签栏

How to create tabbar with custom UI in swift 3.0

我想从情节提要创建一个标签栏,我创建了它,但是当时点击标签栏时图像不显示,有些图像 我想要标签栏,例如

我明白了

我点击任何标签栏项目时,它的显示像

这是我在配置文件视图控制器中使用的代码

class ProfileViewController: UIViewController {
 override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.statusBarStyle = .default

        self.tabBarController?.tabBar.isHidden = false
        UITabBar.appearance().tintColor = UIColor.init(patternImage: UIImage.init(named: "ic_home_tab_profile_sel.png")!)
        // Do any additional setup after loading the view.
    }
}

如有任何帮助,我们将不胜感激。 提前谢谢你。

我建议使用 ESTabBarControllerExample https://github.com/eggswift/ESTabBarController 进行这种自定义 TabbarController.First 从 github 下载 ESTabbarControllerExample。我们需要用到一些class字母。让我一步步解释如何使用:

  • 首先安装 CocoaPods

    1 打开终端并 cd ~ to your project directory

    2 运行 命令 - pod init

    3 您的 podfile 应该与 - pod "ESTabBarController-swift" 一起使用并保存它

    4 然后用命令安装它 pod install

  • 打开.xcworkspace扩展名的项目文件

    1 在项目中我们需要添加Content all swift class and pop.framework

    2 不要使用添加文件来添加 pop.framework。您必须从框架添加并添加其他。

    3 Content文件夹中的所有文件import ESTabBarController_swift

  • StoryBord 东西

    1 添加导航控制器并从 EST 演示的示例代码中添加 ExampleNavigationController。 (您也可以添加自己的)但请确保设置其 Class 导航自定义 swift class.

  • AppDelegate.swift

  • 处的代码资料

您需要在 didFinishLaunchingWithOptions

中执行以下代码
            let tabBarController = ESTabBarController()
            tabBarController.delegate = self
            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"


        self.window?.rootViewController = navigationController

        return true

为 Tabbar 项目和您想要在资产中使用的其他项目添加图像。 希望对你有所帮助。

示例项目:https://github.com/nitingohel/CustomTabCenterBig