Info button as UIBarButtonItem doesn't respond to taps - ios

I'm trying to programmatically add and info button to my app's navigation. However, it doesn't ever fire the action that I'm registering to it.
UIBarButtonItem* infoButton =[[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]];
[infoButton setTarget:self];
[infoButton setAction:#selector(infoButtonTapped)];
The button shows up in my nav bar, but clicking on it doesn't pass into infoButtonTapped

Add the target/action to the UIButton. The bar button item behaves differently when it's set up from a UIButton.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];

Related

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

UIBarButtonItem Custom view in UINavigationBar

I am making a custom bar buttons for uinavigationbar, I am using following code
UIImageView *backImgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chk_back.png"]]; [backImgView setUserInteractionEnabled:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backImgView];
[backButton setTarget:self];
[backButton setAction:#selector(BackBtn)];
checkIn_NavBar.topItem.leftBarButtonItem = backButton;
The problem is it is not calling its action. What i am doing wrong?
From Apple documentation:
Initializes a new item using the specified custom view.
- (id)initWithCustomView:(UIView *)customView
Parameters
customView
A custom view representing the item.
Return Value
Newly initialized item with the specified properties.
Discussion:
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
Solution: "Create button with your background image (set action to this button) and init bar button with this button". For example:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,25,25)
[btn setBackgroundImage:[UIImage imageNamed:#"chk_back.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BackBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
I accomplished this using following code:
UIButton *nxtBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[nxtBtn setImage:[UIImage imageNamed:#"chk_next.png"] forState:UIControlStateNormal];
[nxtBtn addTarget:self action:#selector(NextBtn) forControlEvents:UIControlEventTouchUpInside];
[nxtBtn setFrame:CGRectMake(0, 0, 64, 31)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithCustomView:nxtBtn];
checkIn_NavBar.topItem.rightBarButtonItem=nextButton;

How to prevent animation of back button when switching views?

I cannot figure out how to disable the back-button animation that occurs in the navigation bar when switching from a tableview to a standard view (when a cell is selected). There is no obvious line of code that enabled animation to begin with. Here it is in gif-form:
The navigation buttons in the Facebook app do not animate, so it is possible.
It may be relevant to mention that I am using the ViewDeck library to create the Facebook-like tableView menu, i.e. swipe to the right to expose a table.
EDIT: solution is based on Hesham Abd-Elmegid's answer but modified to use a custom image...
UIImage *settingsImage = [UIImage imageNamed:#"back_button#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
[backButton setBackgroundImage:settingsImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
If you set a custom UIBarButtonItem as a left navigation item (instead of standard back button item), it will fade instead of slide in, just like in Facebook's app. Just create a simple method that will replace back button functionality by calling popViewControllerAnimated: on the navigation controller in which your detail view controller is contained.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack)] autorelease];
}
- (void)goBack
{
[self.navigationController popViewControllerAnimated:YES];
}
Note: UIBarButtonItem can also be set up with an image using initWithImage:style:target:action: method.
You could replace the back button with a custom UIButton. That way it won't animate on transition.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Back" forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 50, 30);
[backButton addTarget:self action:#selector(onBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
You will have to find a PNG for the arrow shape of the back button though.

Setup Sharekit's share button with UIButton, not UIBarButton

In ShareKit's install instructions here: http://getsharekit.com/install/
they say to create a share button by putting this in the .m
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(share)]
How can I set up a share button with just a UIButton, not a UIBarButton?
Create your UIBUtton normall.
Then for the addTarget method use share
so
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(.....
[btn addTarget:self action:#selector(share) forControlEvents:UIControlEventTouchUpInside];

Dismiss ModalView Programmatically by done button

I have to click on done button to dismiss modalview programmatically. I think UIButton is better than UIBarButtonItem to add UIControlEventsTouchupInside.
But with UIButton I'm confused what buttontype should use.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(dismissViewaction:)] autorelease];
You will most likely want to use UIButtonTypeRoundedRect or UIButtonTypeCustom
With the custom type, you can add images for display.
You can try "stealing" these images from the UIBarButtonItem (image property defined in UIBarItem) and making the custom button look like the UIBarButtonSystemItemDone button
UIBarButtonItem * buttonForImage = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonForImage.image forState:UIControlStateNormal];
Things to look out for. When setting an image for a UIButton, it does not scale to the size of the button according to contentMode property. If you want the image to follow the rules of the contentMode property, use setBackgroundImage: forState: instead.
I think the type of button doesn't matter. You have to assign an action or a selector to the button.
Something like this:
UIBarButtonItem *bttItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(yourBttAction:)] autorelease];
the action:
- (IBAction) yourBttAction:(id)sender
{
NSLog(#"Done Button clicked");
//do something
}
If the button is on the modalViewController, I usualy, for dismiss it, I use:
[self dismissModalViewControllerAnimated:(BOOL)];
or
//if you have a navigationController
[self.navigationController dismissModalViewControllerAnimated:(BOOL)];
But if you want to use a delegate to dismiss it, take a look at this tutorial

Resources