Swift控件--UITabBarController

UITabbarController的创建及设置
tabBarItem图片的推荐尺寸和最大支持尺寸
下面是标签栏(UITabBar)中tab按钮图标分别在1x、2x、3x下不会压缩变形的尺寸:
@1x : 推荐 25 x 25 (最大: 48 x 32)
@2x : 推荐 50 x 50 (最大: 96 x 64)
@3x : 推荐 75 x 75 (最大: 144 x 96)

class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //图片和文字颜色一致
        tabBar.tintColor = UIColor.orange

        //只改变文字的颜色
        //UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.gray], for: .normal)
        //UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        //只改图片颜色时,先将所有图片和按钮设置成同一种颜色,在将文字的颜色还原
        //UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : view.tintColor], for: .selected)
        //创建tabbar的子控制器
       createSubViewControllers()
        //设置默认选中的控制器
        selectedIndex = 0
        //设置tabBar的背景颜色  创建一个带有背景色的view插入到tabBar
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 49))
        bgView.backgroundColor = UIColor.white
        tabBar.insertSubview(bgView, at: 0)
    }
    
    func createSubViewControllers() -> Void {
        let oneVC = OneVC()
        //修改渲染模式 为图片原始状态
        let itemOne = UITabBarItem(title: "首页", image: UIImage(named: "home")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "home_bg"))
        oneVC.tabBarItem = itemOne
         let oneNvc = UINavigationController(rootViewController: oneVC)
        
        let twoVC = TwoVC()
        let itemTwo = UITabBarItem(title: "发现", image: UIImage(named: "paper")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "paper_bg"))
        twoVC.tabBarItem = itemTwo
        let twoNvc = UINavigationController(rootViewController: twoVC)
        
        let threeVC = ThreeView()
        let itemThree = UITabBarItem(title: "消息", image: UIImage(named: "info")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "info_bg"))
        threeVC.tabBarItem = itemThree
        let threeNvc = UINavigationController(rootViewController: threeVC)
        
        let fourVC = FourVC()
        let itemFour = UITabBarItem(title: "更多", image: UIImage(named: "more")?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: "more_bg"))
        fourVC.tabBarItem = itemFour
        let fourNvc = UINavigationController(rootViewController: fourVC)
        
        let tabArray = [oneNvc, twoNvc, threeNvc, fourNvc]
        viewControllers = tabArray
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

你可能感兴趣的:(Swift控件--UITabBarController)