I need to change the selection color of UITabBar from default blue to red. How do we do this.
相关问题
- 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?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
Starting from iOS 8 it's as simple as:
I've been searching for a way to set the selected text color of a UITabBarItem and have found a dead simple method, using the UIAppearance protocol.
Please excuse the awful colors!
Update September 2017: It's been two years since I've written this answer and since it's receiving upvotes regularly, I should say this is probably the worst possible answer to this question, it's error prone, likely to break because of iOS updates, hard to debug, etc., so please don't do the things I've written and apply better solutions such as subclassing UITabBar or UITabBarController. Thanks.
You can do this by setting a "tintColor" attribute (Key Path) for you UITabBar.
This should do it. You can check it against the screenshot below.
More on this: There's a "Tint" attribute in Identity Inspector of UITabBar which I believed would do the exact same thing but apparently, it does nothing. It's default value is the exact default fill color when a UITabBarItem is selected, so my guess is it would be fixed in the stable release Xcode 7. Fingers crossed.
In IOS5, UITabBar has a selectedImageTintColor property which does what you need.
To achieve above result perform following steps.
Step 1: Add your desired images in
Assets.xcassets
, and make sure theyRender As
:Default
Step 2: Select your
UITabBar
object and setImage Tint
color, this color will be selected tab colorStep 3: Select
UITabBar
object and add Key Path:unselectedItemTintColor
, Type:Color
, Value:Choose color for unselected item
in User Defined Runtime Attributes.All done.
The SDK does not make this easy, but it is technically possible. Apple apparently believes this to be part of their vision of a consistent look and feel.
UITabBar is a subclass of UIView. You can always subclass and implement your own
-drawRect:
This is not a trivial task, however, you have to essentially re-implement the class from scratch or you risk some weird side-effects.