如何让UITabBarselect指标图像填充整个空间?

我有一个UITabBarController我用这个代码来设置select指标图像:

let selectedBG = UIImage(named:"tabbarbgtest.png")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 0, 0, 0)) UITabBar.appearance().selectionIndicatorImage = selectedBG 

但图像不能填满整个空间 – 请参阅下图:

在这里输入图像说明

图像只是一个82x49px的解决scheme的红色正方形,但具有更广泛的形象,它仍然不能填补整个空间。 希望你们能帮忙 – 谢谢。

如果你想设置选项卡select红色图像,我会build议你设置活动标签栏项目的背景颜色,根据我的意见,每当你可以做你的东西与编码为什么使用图像,所以它更好地设置select颜色,而不是图片。 你可以使用下面的代码来实现。

 // set red as selected background color let numberOfItems = CGFloat(tabBar.items!.count) let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height) tabBar.selectionIndicatorImage = UIImage.imageWithColor(UIColor.redColor(), size: tabBarItemSize).resizableImageWithCapInsets(UIEdgeInsetsZero) // remove default border tabBar.frame.size.width = self.view.frame.width + 4 tabBar.frame.origin.x = -2 

利用UIImage的以下扩展:

 extension UIImage { class func imageWithColor(color: UIColor, size: CGSize) -> UIImage { let rect: CGRect = CGRectMake(0, 0, size.width, size.height) UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(rect) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

希望能帮到你。

或者另一种方式在这里:

在你的tabBarController中,你可以设置默认的UITabBar tintColor,barTintColor,selectionIndicatorImage(在这里作弊一点)和renderingMode的图像,见下面的注释:

  class MyTabBarController: UITabBarController, UINavigationControllerDelegate { ... override func viewDidLoad() { ... // Sets the default color of the icon of the selected UITabBarItem and Title UITabBar.appearance().tintColor = UIColor.redColor() // Sets the default color of the background of the UITabBar UITabBar.appearance().barTintColor = UIColor.blackColor() // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar) UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height)) // Uses the original colors for your images, so they aren't not rendered as grey automatically. for item in self.tabBar.items as! [UITabBarItem] { if let image = item.image { item.image = image.imageWithRenderingMode(.AlwaysOriginal) } } } ... } 

而且您将需要扩展UIImage类来制作您所需大小的普通彩色图像:

 extension UIImage { func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage { UIGraphicsBeginImageContextWithOptions(size, false, 0) color.setFill() UIRectFill(CGRectMake(0, 0, size.width, size.height)) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

截至2017年,老实说皮尤什的答案不适合我。 经过几天寻找另一个解决scheme,我发现我的 – by subclassing the UITabBarController.

即使旋转,它也适用于多个设备。

备注

  1. 使您的图像渲染模式为Original
  2. 将这个类下面的代码分配给Storyboard中的UITabBarController,或者如果以编程方式执行屏幕,则将其作为基类。

     // // BaseTabBarController.swift // MyApp // // Created by DRC on 1/27/17. // Copyright © 2017 PrettyITGirl. All rights reserved. // import UIKit class BaseTabBarController: UITabBarController { let numberOfTabs: CGFloat = 4 let tabBarHeight: CGFloat = 60 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) updateSelectionIndicatorImage() } override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() updateSelectionIndicatorImage() } func updateSelectionIndicatorImage() { let width = tabBar.bounds.width var selectionImage = UIImage(named:"myimage.png") let tabSize = CGSize(width: width/numberOfTabs, height: tabBarHeight) UIGraphicsBeginImageContext(tabSize) selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height)) selectionImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() tabBar.selectionIndicatorImage = selectionImage } } 

你应该看看这个 ,使tabbarbgtest.png可resize,然后将图像分配给selectionIndicatorImage,你甚至可以在故事板编辑器中做到这一点。