微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift中UITabbarController的使用

func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        // 视图1
        let vc01 = UIViewController()
        vc01.title = "视图1"
        vc01.view.backgroundColor = UIColor.greenColor()
        let nav01 = UINavigationController(rootViewController: vc01)
        // 视图2
        let vc02 = UIViewController()
        vc02.title = "视图2"
        vc02.view.backgroundColor = UIColor.yellowColor()
        let nav02 = UINavigationController(rootViewController: vc02)
        // 视图3
        let vc03 = UIViewController()
        vc03.title = "视图3"
        vc03.view.backgroundColor = UIColor.orangeColor()
        let nav03 = UINavigationController(rootViewController: vc03)
        // 视图4
        let vc04 = UIViewController()
        vc04.title = "视图4"
        vc04.view.backgroundColor = UIColor.brownColor()
        let nav04 = UINavigationController(rootViewController: vc04)
        // 视图5
        let vc05 = UIViewController()
        vc05.title = "视图5"
        vc05.view.backgroundColor = UIColor.blueColor()
        let nav05 = UINavigationController(rootViewController: vc05)
        // 视图6
        let vc06 = UIViewController()
        vc06.title = "视图6"
        vc06.view.backgroundColor = UIColor.redColor()
        let nav06 = UINavigationController(rootViewController: vc06)
        
        // tabbarController
        let tabbarController = UITabBarController()
        tabbarController.tabBar.barTintColor = UIColor.blackColor()
        // 注意:视图控制器超过5个时(不包含5)会自动生成一个more视图标签,用来控制第5、6、...以后的视图控制器。
        tabbarController.viewControllers = [nav01,nav02,nav03,nav04,nav05,nav06]
        // 属性设置
        // 设置认被选中视图控制器
        tabbarController.selectedindex = 0;
        // 设置切换视图 tabBar 属性
        // 1 打开用户交互
        tabbarController.tabBar.userInteractionEnabled = true;
        // 2 设置背景颜色
        tabbarController.tabBar.backgroundColor = UIColor.whiteColor()
        // 3 设置背景图片
        tabbarController.tabBar.backgroundImage = UIImage(named: "")
        // 4 选中时的背景图片
        tabbarController.tabBar.selectionIndicatorImage = UIImage(named: "")
        
        // 设置标题,未选中状态图标,选中状态图标
        let barItem01 = UITabBarItem(title: "第1视图",image: UIImage(named: "tabbar01_normal"),selectedImage: UIImage(named: "tabbar01_selected"))
        vc01.tabBarItem = barItem01
        let barItem02 = UITabBarItem(title: "第2视图",image: UIImage(named: "tabbar02_normal"),selectedImage: UIImage(named: "tabbar02_selected"))
        vc02.tabBarItem = barItem02
        let barItem03 = UITabBarItem(title: "第3视图",image: UIImage(named: "tabbar03_normal"),selectedImage: UIImage(named: "tabbar03_selected"))
        vc03.tabBarItem = barItem03
        let barItem04 = UITabBarItem(title: "第4视图",image: UIImage(named: "tabbar04_normal"),selectedImage: UIImage(named: "tabbar04_selected"))
        vc04.tabBarItem = barItem04
        let barItem05 = UITabBarItem(title: "第5视图",image: UIImage(named: "tabbar05_normal"),selectedImage: UIImage(named: "tabbar05_selected"))
        vc05.tabBarItem = barItem05
        let barItem06 = UITabBarItem(title: "第6视图",image: UIImage(named: "tabbar06_normal"),selectedImage: UIImage(named: "tabbar06_selected"))
        vc06.tabBarItem = barItem06
      
        // 设置字体颜色
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()],forState: UIControlState.normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.greenColor()],forState: UIControlState.Selected)
        // 设置字体大小
        UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(8.0)],forState: UIControlState.normal)
        // 设置字体偏移
        UITabBarItem.appearance().titlePositionAdjustment = UIOffsetMake(0.0,-5.0)
        // 设置图标选中时颜色
        UITabBar.appearance().tintColor = UIColor.redColor()
        
        self.window!.rootViewController = tabbarController
        self.window!.makeKeyAndVisible()
        
        
        return true
    }


原文地址:https://www.jb51.cc/swift/322830.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐