Swift选项卡栏项目颜色和背景色
我试图在TabBarController中设置TabBar上单个项目的图标颜色和背景颜色 我的标签栏上有5个图标,其中4个我已经设置了颜色,这只是我正在努力解决的最后一个图标。我在下面附上了一个图片链接,展示了我正在努力实现的目标Swift选项卡栏项目颜色和背景色,swift,ios8,uitabbarcontroller,Swift,Ios8,Uitabbarcontroller,我试图在TabBarController中设置TabBar上单个项目的图标颜色和背景颜色 我的标签栏上有5个图标,其中4个我已经设置了颜色,这只是我正在努力解决的最后一个图标。我在下面附上了一个图片链接,展示了我正在努力实现的目标 特定的图标是图像中间的一个,用于我的相机馈送,这个图标我想是白色的,背景是红色的。 到目前为止,我的代码是这样的- 在我的TabBarController上: var imageNames = ["FeedIcon", "ExploreIcon", "CameraIc
特定的图标是图像中间的一个,用于我的相机馈送,这个图标我想是白色的,背景是红色的。 到目前为止,我的代码是这样的- 在我的TabBarController上:
var imageNames = ["FeedIcon", "ExploreIcon", "CameraIcon", "ActivityIcon", "MyProfileIcon"]
let tabItems = tabBar.items as! [UITabBarItem]
for (index, value) in enumerate(tabItems)
{
var imageName = imageNames[index]
value.image = UIImage(named: imageName)
value.imageInsets = UIEdgeInsetsMake(5.0, 0, -5.0, 0)
}
//for below i've used a extension for imageWithColor to set the color of my icons
for tabItems in self.tabBar.items as! [UITabBarItem] {
if let image = tabItems.image {
tabItems.image = image.imageWithColor(UIColor(red: 57.0/255.0, green: 57.0/255.0, blue: 57.0/255.0, alpha: 1.0)).imageWithRenderingMode(.AlwaysOriginal)
}
}
我的分机号码:
extension UIImage {
func imageWithColor(tintColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
let context = UIGraphicsGetCurrentContext() as CGContextRef
CGContextTranslateCTM(context, 0, self.size.height)
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context, rect, self.CGImage)
tintColor.setFill()
CGContextFillRect(context, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
可能有点晚了,但要创建此相机项目,我认为唯一的解决方案是创建一个UIButton并将按钮的中心设置为UIAbbar的中心。你可以看到如何做到这一点。之后,您可以将图像设置为具有此背景色的按钮 更新: 我用swift创建了这个,你可以看到如何在选项卡栏上添加这个按钮 主要问题是将UITabBarController子类化,并从这个类中添加按钮
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewDidAppear(animated: Bool) {
// Creates image of the Button
let imageCameraButton: UIImage! = UIImage(named: "cameraIcon")
// Creates a Button
let cameraButton = UIButton(type: .Custom)
// Sets width and height to the Button
cameraButton.frame = CGRectMake(0.0, 0.0, imageCameraButton.size.width, imageCameraButton.size.height);
// Sets image to the Button
cameraButton.setBackgroundImage(imageCameraButton, forState: .Normal)
// Sets the center of the Button to the center of the TabBar
cameraButton.center = self.tabBar.center
// Sets an action to the Button
cameraButton.addTarget(self, action: "doSomething", forControlEvents: .TouchUpInside)
// Adds the Button to the view
self.view.addSubview(cameraButton)
}
func doSomething() {
print("Action of camera button")
}
}
谢谢@angeldev,我会看看这个,并告诉你它是否解决了问题。@ggomersall我刚刚在我的一个项目中试用过,它对我很有效。我也和斯威夫特一起做了。如果你有任何问题,请告诉我:。太棒了!!我会让你知道的