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

后端 未结 5 1705
花落未央
花落未央 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 21:21

    In swift 4 and 5 you can use the below extension. Remember one thing always pass the same number of images , selected images and title but if you do not want to set title then pass nil in title.

    extension UITabBarController{

        func setUpImagaOntabbar(_ selectedImage : [UIImage], _ image : [UIImage], _ title : [String]?){
    
            for (index,vals) in image.enumerated(){
    
                if let tab = self.tabBar.items?[index]{
    
                    tab.image = image[index]
                    tab.image = selectedImage[index]
                    if let tile = title[index]{
                       tab.title = title[index]
                      }
    
                }
            }
        }
    }
    

提交回复
热议问题