I want to change background color of UITabBarItem
badge but can't find any resource on how to make it.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
It appears that no. You may only set the value. From Apple's documentation badge is:
Take a look here @ UITabbarItem-CustomBadge.
A complete demonstration is following
it takes only two line of code, if you want to use the default implementation
I wrote this piece of code for my app, but I have only tested it in iOS 7.
You're only able to update the badge background with an image, not a color. I have also exposed the badge label if you wanted to update that in some way.
Its important to note that this code must be called after setting the
tabBarItem.badgeValue
!EDIT: 4/14/14
The above code will work in iOS 7 when called anywhere. To get it working in iOS 7.1 call it in the view controllers
-viewWillLayoutSubviews
.EDIT: 12/22/14
Here's an updated snippet which I'm currently using. I put the code in a category extension for simplicity.
Then when you're ready to call it, it'll look like this.
EDIT: 11/16/15
It's been brought to my attention that some people need a little more clarity on what's happening in this code. The for loops are searching for a few views which are not publicly accessible. By checking if the views class name contains a part of the expected name, it's ensuring to reach the intended view while not setting off any possible red flags by Apple. Once everything has been located, a block is executed with easy access to these views.
It's noteworthy that the possibility exists for this code to stop working in a future iOS update. For example these internal views could one day acquire different class names. However the chances of that are next to none since even internally Apple rarely refactors classes to this nature. But even if they were to, it would be something along the title of
UITabBarBadgeView
, which would still reach the expected point in code. Being that iOS9 is well out the door and this code is still working as intended, you can expect this problem to never arise.No you can't change the color but you can use your own badges instead. Add this extension at the file scope and you can customise the badges however you like. Just call
self.tabBarController!.setBadges([1,0,2])
in any of your root view controllers.To be clear that is for a tab bar with three items, with the badge values going from left to right.
Swift 3 Here is an updated version of @Kirualex's answer (who improved on @TimWhiting's answer) for Swift 3.
You need to specify tab item at index to change badge color, #available in iOS 10 ,