Highlight UIBarButtonItem like iOS7.1 calendar - already tried setBackgroundImage - ios

I'm trying to make a UIBarButtonItem behave like the "view mode" button in the iOS 7 calendar app. When tapped it becomes highlighted and stays this way until it's tapped again.
Example image (can't embed images yet, sorry)
I have already tried setting the BackgroundImage property to a image with forState:(UIControlStateHighlighted | UIControlStateSelected) as suggested here, nothing happens.

I think you should use a custom view. See this example using a 40x40 button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,40,40);
[button setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"buttonSelected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:buttonItem, nil]];
And in the touch set selected
- (void) didTouchButton:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = YES;
//Do stuff
}

Related

How to get this UIBarButtonItem in the picture?

I want to get a button with short lines on UINavigationBar.
Please, see that on this image:
What is it? How to get it?
You'll want to instantiate a UIBarButtonItem with a custom view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 24, 24);
[button setImage:[UIImage imageNamed:#"your_image_filename"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTappedMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
This creates a UIButton with a supplied image. That UIButton is then set as a custom view for a UIBarButtonItem. Then, just set the barButtonItem on your navigation item.

Make custom Navigation Buttons glow when touched?

In my navigation bar I have two right button bar items. One is a custom button and the other uses the info light button type. Both work great but I would like to make the custom button have the default glow the same way the info button does when you touch it. Is this possible?
// Default info light btn
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBtnItem = [[UIBarButtonItem alloc]initWithCustomView:infoBtn];
// Custom Btn
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
[customButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
NSArray *rightBtns = #[customButtonItem, infoBtnItem];
[navItem setRightBarButtonItems:rightBtns animated:YES];
you just need to put one more line of code like:-
customButton.showsTouchWhenHighlighted = TRUE;
After this your code Output something like this:-

Custom navigation bar back button with only image

I want to show custom navigation bar back button. I want to show only image on it. I want to use this button in whole app also dont want to create this button in each file. How can i??
Here is my code:
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"back_arrow.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
but this is showing on current page only.
Another code:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"back_arrow.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Using this code back button image is showing in whole app. But the text "Back" is also showing. How can i solve this problem.
Thanks in advance.
I've used a category in the past to do this.
Create a category on UIBarButtonItem called +projectButtons (or something).
Then you can have a function like...
+ (UIBarButtonItem)backArrowButtonWithTarget:(id)target action:(SEL)action
{
UIImage *buttonImage = [UIImage imageNamed:#"back_arrow.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return customBarItem;
}
Then add it to you navigation bar like so...
self.navigationItem.leftBarButtonItem = [UIBarButtonItem backArrowButtonWithTarget:self action:#selector(popViewControllerAnimated:)];
This means you only need one line of code to create it but you still get the customisation of the target and action in each VC that you use it in. If you decide to change the look of the button then it's all done in one place.

Customizing UIBarButtonItem in Objective C

I am trying to add image to UIBarButtonItem in IOS App. I am able to add it but the problem is image is taking the size of the button. But I want button to be of the size of the image with borders included in it.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundImage:[UIImage imageNamed:#"customButtonBgImage.png"] forState:UIControlStateNormal];
[customButton setImage:[UIImage imageNamed:#"customButtonImage.png"] forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
And the you can add barButtonItem where-ever you need it.

Remove navigation button

Hey, I've written a class (A) which inherits some functionality including an implementation of a navigation button. Class A has both a view and edit mode, I want to only show the button when am in edit mode. So far I've not been able to remove this button and I don't really want to create to another class just for edit.
Also other classes inherit this functionality so I don't really want to be messing about with parent.
The code that I use to create the button is below:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
button.bounds = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setTitle:NSLocalizedString(#"BUTTON", #"")
forState:UIControlStateNormal];
LPRBSLabel *buttonLabel = [[LPRBSLabel alloc] initWithStyle:UICustomeButtonTitle];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, -5.0, 0.0)];
button.titleLabel.font = buttonLabel.font;
[button setTitleColor:buttonLabel.textColor forState:UIControlStateNormal];
[buttonLabel release];
UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;
[barLeftInfoButton release];
I managed to solve it using:
self.navigationItem.leftBarButtonItem = nil;
I had a mind freeze and was using the above statement before the button had actually when created :-(
It would be easier to set the NavigationItem's property "hidesBackButton" to yes:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html#//apple_ref/occ/cl/UINavigationItem
Another option to consider is to disable the button until you want it to function. It will be visible, but dimmed.
self.navigationItem.leftBarButtonItem.enabled = NO;
To Remove Button from navigation bar just assign tag to button and write a below code
[[self.navigationController.navigationBar viewWithTag:0106] removeFromSuperview];

Resources