Swift-UITabBarController适配(iOS13/15)

class MyTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setTabbar()
    }
}

extension MyTabBarController {
  
    // MARK: - 设置tabbar
    func setTabbar() {
        // MARK: TabBar背景颜色
        view.backgroundColor = .white
        UITabBar.appearance().backgroundColor = .white
        
        UITabBar.appearance().isTranslucent = false  //避免受默认的半透明色影响,关闭
        /// 未选中颜色/字号
        let normal = [NSAttributedString.Key.foregroundColor: UIColor.lightGray, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
        /// 选中颜色/字号
        let selected = [NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)]
        
        // MARK: - 去除黑线/修改颜色 (shadowImage/backgroundImage缺一不可)
        if #available(iOS 13.0, *) {
            let appearance = tabBar.standardAppearance
            appearance.backgroundImage = UIImage()
            appearance.backgroundColor = .clear
            appearance.shadowColor = .clear
            appearance.shadowImage = UIImage()
            // appearance.shadowImage = image(UIColor.red) // 修改线条颜色
            tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance
            }
        } else {
            tabBar.shadowImage = UIImage()
            // tabBar.shadowImage = image(UIColor.red) // 修改线条颜色
            tabBar.backgroundImage = UIImage()
        }
        
        // 字体偏移
        let titlePositionAdjustment = UIOffset(horizontal: 0.0,vertical: 0.0)
        
        // MARK: - 设置 tabBar 字体颜色/字号 (未选中/选中)
        if #available(iOS 13.0, *) { // iOS13 无法成功设置tabBar未选中状态下的文字颜色
            let inlineLayoutAppearance = UITabBarItemAppearance()
            inlineLayoutAppearance.normal.titlePositionAdjustment = titlePositionAdjustment // iOS13 设置字体偏移
            inlineLayoutAppearance.normal.titleTextAttributes = normal // iOS13 修改未选中的颜色
            inlineLayoutAppearance.selected.titleTextAttributes = selected // iOS13 修改选中的颜色
            let appearance = tabBar.standardAppearance
            appearance.stackedLayoutAppearance = inlineLayoutAppearance
            tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = appearance
            }
        } else {
            UITabBarItem.appearance().setTitleTextAttributes(normal, for: .normal) // 修改未选中的颜色
            UITabBarItem.appearance().setTitleTextAttributes(selected, for: .selected) // 修改选中的颜色
            UITabBarItem.appearance().titlePositionAdjustment = titlePositionAdjustment // 设置字体偏移
        }
        
        // MARK: - tabBar 底部工具栏背景颜色 (以下两个都行)
        //  tabBar.barTintColor = UIColor.orange
        // 设置图片选中时颜色必须设置(系统默认选中蓝色)
        //  UITabBar.appearance().tintColor = UIColor.lightGray
    }
    
    // MARK: - 根据颜色值画条线
    func image(_ color: UIColor) -> UIImage {
        let rect = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0.5)
        UIGraphicsBeginImageContext(rect.size)
        guard let context = UIGraphicsGetCurrentContext() else {
            UIGraphicsEndImageContext()
            return UIImage()
        }
        context.setFillColor(color.cgColor)
        context.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image ?? UIImage()
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容