问题
I'm trying to set the icon color and background color of an individual item on my TabBar within the TabBarController.
I have 5 icons on my TabBar, 4 of which i've set the color for, it's just this last icon that i'm struggling with. I've attached a link below of an image to show what i'm trying to achieve.
The specific icon is (the one in the middle of the image) for my camera feed, this icon i would like to be white and the background to be red. My code so far is such - On my 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)
}
}
My Extension:
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
}
}
回答1:
Might be a bit late but to create this "camera item" I think the only solution is to create a UIButton and set the center of the button to the center of the UITabBar. You can see how to do it in this post. After that you can set the image to the button with this background color.
UPDATE:
I created this sample project written with swift where you can see how to add this button on the tabBar.
The main issue is to subclass UITabBarController and add the button from this class.
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")
}
}
来源:https://stackoverflow.com/questions/30534409/swift-tabbar-item-color-and-background-color