How to provide normal state and selected/highlighted state images to uibarbuttonitem in iOS 7? Is there any way to provide tint colour for both normal and selected/highlighted state of uibarbuttonitem?
I don't want to use uibutton as a view for uibarbuttonitem! Any elegant solution would be highly appreciated.
You can use a UIButton
as a customView
of a UIBarButtonItem
and have two different background images for a normal
and selected
state of the UIButton. Then when the button is tapped, you can just set the selected
state to YES
or NO
.
// Build right bar button
UIImage *normalButtonImage = [UIImage imageNamed:@"normal-button"];
UIImage *selectedButtonImage = [UIImage imageNamed:@"selected-button"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:@selector(rightBarButtonPress)
forControlEvents:UIControlEventTouchDown];
[orientationButton setShowsTouchWhenHighlighted:YES];
[orientationButton setSelected:NO];
UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setRightBarButtonItem:rightBarButton];
Then your in method for when the button is clicked, change the state
- (void)rightBarButtonPress
{
//toggle selected state of button
UIBarButtonItem *rightBarButton = self.navigationItem.rightBarButtonItem;
UIButton *button = (UIButton *)rightBarButton.customView;
[button setSelected:!button.selected];
//do whatever else you gotta do
}
You can do this using the UIAppearance Protocol Reference
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:options forState:UIControlStateNormal];
You might also have to use:
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
According to this answer, I did something extra and kind of have an answer for you here. I have my custom UITabBarController, which is linked with my UITabBarController in the StoryBoard file. So in order to remove the automatic tint provided by iOS when the TabBar is unselected, I ended up removing it in this manner. The images can be a vast variety of images but just in the way recommended here. Here it goes:
NSArray *navConArr = self.viewControllers;//self is custom UITabBarController
UINavigationController *naviOne = [navConArr objectAtIndex:0];//I have 3 different tabs, objectAtIndex:0 means the first tab navigation controller
UITabBarItem *naviBtn = naviOne.tabBarItem;
UIImage *image = [[UIImage imageNamed:@"iconNaviOne"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[naviBtn setSelectedImage:image];
[naviBtn setImage:image];
So when moving from this tab to the other one and leaving this in the unselected (gray as default) tint, you can set it to the image provided. Thankfully, this works like a charm (:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With