Swift Tabbar纯代码设置

斜成济
2023-12-01

遇到一个很傻X的问题,tabbar设置alwaysOriginal无效,要用tabbar.viewcontrollers里面的控制器设置baritem

// A code block
func createTabbar() {
        
        let unselectedImageArr = ["tab_home","tab_browse","tab_wander","tab_my"]
        let selectedImageArr = ["tab_home_select","tab_browse_select","tab_wander_select","tab_my_select"]
        let titleArr = ["首页","分类","逛市场","我的"]
        
        for i in titleArr.indices {

            let image = UIImage(named: unselectedImageArr[i])
            let selectedImage = UIImage(named: selectedImageArr[i])
            let vc = self.viewControllers![i]
            //看好了是vc的tabbaritem 并不是tabBar.items!
            vc.tabBarItem?.selectedImage = selectedImage?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            vc.tabBarItem?.image = image
            vc.tabBarItem?.title = titleArr[i]

            vc.tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:THEMCOLOR_C11B41], for: UIControl.State.selected)
            vc.tabBarItem?.setTitleTextAttributes([NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13)], for: UIControl.State.normal)
        }
    }
 类似资料: