如何在swift中的标签栏项目中设置图像?

我已经采用了一个视图控制器并将其嵌入到导航控制器中,并且它已经嵌入到标签栏控制器中。 当我试图通过故事板设置图像时,图像不会出现在标签栏图标上。图像名称为25。

在此处输入图像描述

在此处输入图像描述

请建议我该怎么办? 我怎么能以编程方式做到这一点? 我应该为此目的采取适当的图像尺寸? 提前致谢。

添加AppDelegate类:

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 } 

在MainTabbarViewController中

绑定标签栏的sockets:

 @IBOutlet weak var myTabBar: UITabBar? override func viewDidLoad() { super.viewDidLoad() myTabBar?.tintColor = UIColor.white tabBarItem.title = "" setTabBarItems() } 

在此处设置tabbar项定义方法如下:

 func setTabBarItems(){ let myTabBarItem1 = (self.tabBar.items?[0])! as UITabBarItem myTabBarItem1.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem1.selectedImage = UIImage(named: "Selected ")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem1.title = "" myTabBarItem1.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) let myTabBarItem2 = (self.tabBar.items?[1])! as UITabBarItem myTabBarItem2.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem2.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem2.title = "" myTabBarItem2.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) let myTabBarItem3 = (self.tabBar.items?[2])! as UITabBarItem myTabBarItem3.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem3.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem3.title = "" myTabBarItem3.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) let myTabBarItem4 = (self.tabBar.items?[3])! as UITabBarItem myTabBarItem4.image = UIImage(named: "Unselected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem4.selectedImage = UIImage(named: "Selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal) myTabBarItem4.title = "" myTabBarItem4.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) } 

干杯!

你正在以正确的方式做所有的事情但唯一的问题是你的tabbaritem图像的大小不正确。看看这个表格的tabbaritem图像的实际大小。

在此处输入图像描述

设置两个图像 – 用于选择/选择状态

看到这个