如何在 swift 3.0 中创建具有自定义 UI 的选项卡栏

2024-01-04

i want to create one tab bar from storyboard and i create it but when click on tab bar at that time image is not showing and some images i want tab bar like I want like this tabbar

and 我懂了

When 我单击任何选项卡栏项目,其显示如下

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

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.
    }
}

任何帮助都可以感激。 先感谢您。


我建议使用ESTabBarController示例 https://github.com/eggswift/ESTabBarController https://github.com/eggswift/ESTabBarController制作这种自定义 TabbarController。首先从 github 下载 ESTabbarControllerExample。我们需要使用一些班级字母。我来一步步解释一下如何使用:

  • 首先安装CocoaPods

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

    2 运行命令 -pod init

    3 你的 podfile 应该与 -pod "ESTabBarController-swift"并保存它

    4、然后用命令安装pod install

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

    1 在项目中我们需要添加Content https://github.com/eggswift/ESTabBarController/tree/master/ESTabBarControllerExample/ESTabBarControllerExample/Content所有 Swift 级别和pop.framework https://github.com/eggswift/ESTabBarController/tree/master/ESTabBarControllerExample/ESTabBarControllerExample/Framework

    2 不要使用add File to 添加pop.framework。您必须从框架添加并添加其他。

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

  • 故事板的东西

    1 添加导航控制器ane同时添加ExampleNavigationController来自 EST demo 的示例代码。 (您也可以添加自己的)但请确保设置其导航自定义 swift 类的类。

  • 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

为选项卡栏项目和其他要在资产中使用的项目添加图像。 希望对您有帮助。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 swift 3.0 中创建具有自定义 UI 的选项卡栏 的相关文章

随机推荐