Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem Custom view in UINavigationBar

Tags:

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?

like image 970
Hassy Avatar asked Apr 17 '13 10:04

Hassy


People also ask

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

Can you change the navigation bar on Iphone?

Change the Bar StyleA user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I use the navigation bar in Xcode?

Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.

How do I change the navigation bar color in Swift?

navigationController. navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.

Can I use a UIView instead of a uibarbuttonitem?

You can apply a custom UIView to the right side of the navigation bar instead of using a UIBarButtonItem. This part of the sample demonstrates placing three kinds of UIBarButtonItems on the right side of the navigation bar: a button with a title, a button with an image, and a button with a UISegmentedControl.

How do I set the uinavigationbar’s prompt?

Here’s how to set the navigation item’s prompt: Apply a custom background to a UINavigationBar by adding a bar tint color or background image. The sample sets the background image of a navigation bar like this:

How do I apply a custom background to a uinavigationbar?

Apply a custom background to a UINavigationBar by adding a bar tint color or background image. The sample sets the background image of a navigation bar like this: Users can quickly switch between different stack levels with a tap and hold on the back button.

How to position image outside of the uibarbuttonitem?

You can use the image insets to position the image outside of the bounds of the UIBarButtonItem, as well, and the entire vicinity of the left-hand-side button is tappable, so you don't have to worry about ensuring it's positioned near a tap target. (at least, within reason.) Show activity on this post. Show activity on this post.


2 Answers

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];
like image 123
Alexander Merchi Avatar answered Sep 21 '22 13:09

Alexander Merchi


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;
like image 26
Hassy Avatar answered Sep 19 '22 13:09

Hassy