how to set image in a tab bar item in swift?

后端 未结 5 1711
花落未央
花落未央 2020-12-28 20:42

I have taken a view controller & embedded it in a navigation Controller and again this has been embedded in a tab bar controller. when i am trying to set a image via sto

5条回答
  •  暖寄归人
    2020-12-28 20:58

    add AppDelegate class :

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        window=UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = setTabbar()
        self.window?.makeKeyAndVisible()
        window?.backgroundColor=UIColor.white
        return true
    }
    
    func setTabbar() -> UITabBarController
    {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let tabbarcntrl = UITabBarController()
    
        let Home = storyboard.instantiateViewController(withIdentifier: "HomeView") // 1st tab bar viewcontroller
        let Followed = storyboard.instantiateViewController(withIdentifier: "FollowedView") // 2nd tab bar viewcontroller
        let Message = storyboard.instantiateViewController(withIdentifier: "MessageView") // 3rd tab bar viewcontroller
    
        // all viewcontroller embedded navigationbar
        let nvHome = UINavigationController(rootViewController: Home)
        let nvFollowed = UINavigationController(rootViewController: Followed)
        let nvMessage = UINavigationController(rootViewController: Message)
    
        // all viewcontroller navigationbar hidden
        nvHome.setNavigationBarHidden(true, animated: false)
        nvFollowed.setNavigationBarHidden(true, animated: false)
        nvMessage.setNavigationBarHidden(true, animated: false)
    
        tabbarcntrl.viewControllers = [nvHome,nvFollowed,nvMessage]
    
        let tabbar = tabbarcntrl.tabBar
        tabbar.barTintColor = UIColor.black
        tabbar.backgroundColor = UIColor.black
        tabbar.tintColor = UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)
    
        //UITabBar.appearance().tintColor = UIColor.white
        let attributes = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor.white]
        let attributes1 = [NSFontAttributeName:UIFont(name: "Montserrat-Light", size: 10)!,NSForegroundColorAttributeName:UIColor(red: 43/255, green: 180/255, blue: 0/255, alpha: 1)]
    
        UITabBarItem.appearance().setTitleTextAttributes(attributes, for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes(attributes1, for: .selected)
    
    
        let tabHome = tabbar.items![0]
        tabHome.title = "Home" // tabbar titlee
        tabHome.image=UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // deselect image
        tabHome.selectedImage = UIImage(named: "icon_home.png")?.withRenderingMode(.alwaysOriginal) // select image
        tabHome.titlePositionAdjustment.vertical = tabHome.titlePositionAdjustment.vertical-4 // title position change
    
        let tabFoll = tabbar.items![1]
        tabFoll.title = "Followed"
        tabFoll.image=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
        tabFoll.selectedImage=UIImage(named: "icon_fold.png")?.withRenderingMode(.alwaysOriginal)
        tabFoll.titlePositionAdjustment.vertical = tabFoll.titlePositionAdjustment.vertical-4
    
        let tabMsg = tabbar.items![3]
        tabMsg.title = "Message"
        tabMsg.image=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
        tabMsg.selectedImage=UIImage(named: "icon_mail.png")?.withRenderingMode(.alwaysOriginal)
        tabMsg.titlePositionAdjustment.vertical = tabMsg.titlePositionAdjustment.vertical-4
    
        return tabbarcntrl
    }
    

提交回复
热议问题