Set a button to right of backbutton of navigation bar iPad monotouch - ios

I want to have a uibarbuttonitem in right of back button in navigation bar ( if it is not wrong with apples guide line). I use this code:
UIBarButtonItem listButton = new UIBarButtonItem ("List View",UIBarButtonItemStyle.Bordered ,null);
//assigning left bar buttons
this.NavigationItem .LeftBarButtonItems =
new UIBarButtonItem[] { this.NavigationItem .BackBarButtonItem ,listButton } ;
But this.NavigationItem .BackBarButtonItem always is null. What solution is right for this purpose?

This is the correct implement the same :
UIButton *leftButtonIcon = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, width, height)];
[leftButtonIcon setImage:[UIImage imageNamed:#"yourButtonImageName.png"] forState:UIControlStateNormal];
[leftButtonIcon addTarget:self.viewDeckController action:#selector(YourSelectorMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftbtn = [[UIBarButtonItem alloc] initWithCustomView:leftButtonIcon];
[self.navigationItem setLeftBarButtonItem:leftbtn];

Related

backBarButtonItem not getting displayed

I know viewcontroller's navigation item's backBarButtonItem gets displayed when another view controller is pushed on stack and this is 2nd viewcontroller from top.
I have viewcontroller A which have following in viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = NO;
[self.navigationItem setBackBarButtonItem:[UIBarButtonItem itemWithImageNamed:#"ic_header_slide" selectedImage:nil target:nil action:nil]];
}
When I push viewcontroller B, this custom back button is not getting displayed, instead I see default back button which iOS creates.
A extends UITableViewController and B extends UIViewController. I am not setting leftBarButtonItem, leftBarButtonItems, rightBarButtonItem, rightBarButtonItems in any of these navigationItem.
EDIT
I have read about setting leftBarButtonItems. setting leftbarbuttonitems on B works. but I think setting backBarButtonItem on A is correct way of doing that. It is also mentioned in documentation but not working in my case. I want to ask whether there is bug in backBarButtonItem or I have some misunderstanding the way it works and I am not doing it correctly.
To hide the default back button of the navigation bar use,
self.navigationItem.hidesBackButton=TRUE;
Also use the following method to add the custom BarButtons,
- (NSArray*)getLeftNavButtons:(NSString*)image andTarget:(id)target andFrame:(CGRect)frame andSpace:(int)fixedSpace
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[button addTarget:target action:#selector(leftNavBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7"))
{
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = fixedSpace;
return #[negativeSpacer,barButton];
}
else{
return #[barButton];
}
return #[barButton];
}
Just override the default
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc]init];
back.title = #"Pick Me";
back.image = #"Your image";
[self.navigationItem setLeftBarButtonItem:back];
Set Right Bar Button Item
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];
UIImage *img1=[UIImage imageNamed:#"edit"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:#selector(btnEditClicked:)
forControlEvents:UIControlEventTouchUpInside];
// [signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.rightBarButtonItem=barButton;
Set Left Bar Button Item
UIImage *img11=[UIImage imageNamed:#"home"];
CGRect frameimg11 = CGRectMake(0, 0, img11.size.width, img11.size.height);
UIButton *signOut1=[[UIButton alloc]initWithFrame:frameimg11];
[signOut1 setBackgroundImage:img11 forState:UIControlStateNormal];
[signOut1 addTarget:self action:#selector(showLeftMenuPressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton1=[[UIBarButtonItem alloc]initWithCustomView:signOut1];
self.navigationItem.leftBarButtonItem=barButton1;
self.navigationController.navigationBar.barTintColor=ColorNav;
self.navigationController.navigationBar.translucent=FALSE;
Set Navigation Title & Color
self.title = titletext;
[[[self navigationController] navigationBar]setTitleTextAttributes:#{NSForegroundColorAttributeName: textColor}];

Changing Navigatiobar back button action in ios6/ ios 7 compatible applications

I have a navigation controller application, and I need to set the custom action for the navigation back bar button. Tried some workarounds and not yet to find a solution.
Tried
UIBarButtonItem *backBarItem = self.navigationItem.leftBarButtonItem;
backBarItem.target = self;
backBarItem.action = #selector(popToHomeViewController);
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:backBarItem.title style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result : No effect. Back button pops to just previous viewController in navigation stack
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:backBarItem.style target:self action:#selector(popViewController)];
self.navigationItem.leftBarButtonItem = customBarItem;
Result:Now my selector got invoked perfectly and navigated to desired viewController. Here the issue is that the back button not as like as the native back button. It is not having the bold "<" character as I have not mentioned it. If added < character it needs to be changed for ios 6 compatibility.
Any better solution to ensure ios 6 and ios 7 compatible navigation back button with custom selector?
Try this simple example will help you..
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Make sure you have an button image with the size of a navigation bar back button in your resource folder with name back.png
Feel free if any other assistance is required.
Happy Coding!!!!!
Try this
viewController.navigationItem.hidesBackButton = YES;
//set custom image to button if needed
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:backButtonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[button addTarget:viewController action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIView *backButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height)];
[backButtonView addSubview:button];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButtonView];
viewController.navigationItem.leftBarButtonItem = customBarItem;
and in back method you can customise
- (void)back {
//Your code
}

Create UIBarButtonItem with back button style

I'm looking for a way to create programmatically an UIBarButtonItem that looks like a back button of UINavigationBar.
Apparently seems like that the back button appears only after a push on the UINavigationController.
So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.
Ideas ?
the short answer is you cannot do it.
you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar.
I think you need to use image. and then set the image be the buttons backgroundImage. such as :
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar
It gives you a psd file with image that you need.
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(custom text with back arrow)

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.

UIButton not showing in iOS

Following is a code to display a button to the toolbar.
UIButton myButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *myImage = [UIImage imageNamed:#"img1.png"];
[myButton setBackgroundImage:myImage forState:UIControlStateNormal];
UIBarButtonItem *myToolbarButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
Nothing gets displayed on the toolbar. Please tell me what is wrong.
[myToolbar setItems:[NSArray arrayWithObject:myToolbarButton]]; // might help
OR if it's a navigation bar
self.navigationItem.rightBarButtonItem = myToolbarButton;
Also you should set the frame for the custom button, something like this:
UIButton *mybutton = [[UIButton alloc] initWithFrame:CGRectMake:0,0,200,50];
Then if you want to add a selector/callback:
[myButton addTarget:self action:#selector(someMethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = myButtonItem; // and change for the right button.
Where self is a UIViewController.
You forgot to add this button to the toolbar!
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myToolbarButton,nil]; // you can more buttons here
[aToolbar setItems:toolbarItems animated:NO]; // change to YES if you want to have nice animation
[toolbarItems release];
If you want to add a button to navigation bar, use this:
[self.navigationItem setRightBarButtonItem:myToolbarButton]; // or setLeft...
Also, you should remember to set the frame of myButton:
[myButton setFrame:CGRectMake(0, 0, 30, 32)];

Resources