swift中UITabbarController的使用

前端之家收集整理的这篇文章主要介绍了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.f2er.com/swift/322830.html

猜你在找的Swift相关文章