Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set UIBarButtonItem selected or highlighted image or tint colours in iOS 7?

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.

like image 219
U. Ali Avatar asked Oct 11 '13 14:10

U. Ali


3 Answers

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
}
like image 61
Kevin_TA Avatar answered Nov 16 '22 05:11

Kevin_TA


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]];
like image 45
TrevorL Avatar answered Nov 16 '22 04:11

TrevorL


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 (:

like image 1
Mohsin Khubaib Ahmed Avatar answered Nov 16 '22 04:11

Mohsin Khubaib Ahmed