美文网首页
Swift创建系统tabbar,系统导航栏

Swift创建系统tabbar,系统导航栏

作者: 曼纪珂 | 来源:发表于2018-01-19 16:33 被阅读0次

    1.首先创建swift工程,创建RootViewController继承于UITabBarController

    2.创建BaseViewController继承于UIViewController,初始化导航栏

    如下所示:

    创建几个自己需要的VC,分别继承于BaseViewController

    然后再rootviewcontroller中初始化tabbar有关的信息

        var communityNav: UINavigationController!

        var storeNav: UINavigationController!

        var shoppingMallNav: UINavigationController!

        var mineNav: UINavigationController!

    小编创建的CommunityViewController  StoreViewController   ShoppingMallViewController  MineViewController以这几个为例。

    废话不多说,直接上代码:

    在viewdidload中

    communityNav = UINavigationController (rootViewController: CommunityViewController ())

            communityNav.tabBarItem.title = "社区"

            communityNav.tabBarItem.image = UIImage (named: "tab_community_icon_nor")?.withRenderingMode(.alwaysOriginal)

            communityNav.tabBarItem.selectedImage = UIImage (named: "tab_community_icon_pre")?.withRenderingMode(.alwaysOriginal)

            communityNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .normal)

            communityNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.yellow, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .selected)

            storeNav = UINavigationController (rootViewController: StoreViewController ())

            storeNav.tabBarItem.title = "门店"

            storeNav.tabBarItem.image = UIImage (named: "tab_store_icon_nor")?.withRenderingMode(.alwaysOriginal)

            storeNav.tabBarItem.selectedImage = UIImage (named: "tab_store_icon_pre")?.withRenderingMode(.alwaysOriginal)

            storeNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .normal)

            storeNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.yellow, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .selected)

            shoppingMallNav = UINavigationController (rootViewController: ShoppingMallViewController ())

            shoppingMallNav.tabBarItem.title = "商城"

            shoppingMallNav.tabBarItem.image = UIImage (named: "tab_mall_icon_nor")?.withRenderingMode(.alwaysOriginal)

            shoppingMallNav.tabBarItem.selectedImage = UIImage (named: "tab_mall_icon_pre")?.withRenderingMode(.alwaysOriginal)

            shoppingMallNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .normal)

            shoppingMallNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.yellow, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .selected)

            mineNav = UINavigationController (rootViewController: MineViewController ())

            mineNav.tabBarItem.title = "我的"

            mineNav.tabBarItem.image = UIImage (named: "tab_user_icon_nor")?.withRenderingMode(.alwaysOriginal)

            mineNav.tabBarItem.selectedImage = UIImage (named: "tab_user_icon_pre")?.withRenderingMode(.alwaysOriginal)

            mineNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.red, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .normal)

            mineNav.tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.yellow, NSAttributedStringKey.font : UIFont.systemFont(ofSize: 11)], for: .selected)

            viewControllers = [communityNav, storeNav, shoppingMallNav, mineNav]

    附图一张

    在appdelegate中把rootviewcontroller指定为 window?.rootViewController 的rootviewctroller  即:

    window?.rootViewController = RootViewController()   大功告成,谢谢支持!!(小编也新手,大家多多指点)

    相关文章

      网友评论

          本文标题:Swift创建系统tabbar,系统导航栏

          本文链接:https://www.haomeiwen.com/subject/cvtaoxtx.html