backBarButtonItem not getting displayed - ios

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}];

Related

UIBarButtonItem with UIButton as custom view doesn't show up

I am trying to display a custom UIButton as a UINavigationItem's rightBarButtonItem.
I wrote the following code:
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:#"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 30, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
viewController.navigationItem.rightBarButtonItem = inviteButtonContainer;
Nothing shows up in my navigation bar.
However, with the following code, I can see a basic button:
UIBarButtonItem *inviteButton = [[UIBarButtonItem alloc] initWithTitle:#"Invite"
style:UIBarButtonItemStyleDone
target:self
action:nil];
viewController.navigationItem.rightBarButtonItem = inviteButton;
But it's obviously not what I am after, since I am looking to play with the button appearance.
Edit
I was trying to add the same instance of UIBarButtonItem (inviteButtonContainer) to many navigation bars. Doing that, it's visible on the last navigation bar only.
However, if I use the same instance of a simple UIBarButtonItem (without custom view), it's displayed on every navigation bars. Any idea why?
Your code is fine, just change this line:
viewController.navigationItem.rightBarButtonItem = inviteButtonContainer;
to
self.navigationItem.rightBarButtonItem = inviteButtonContainer;
From the Documentation:
The navigation item used to represent the view controller in a
parent's navigation bar.
self itself represents that it belongs to view controller , so there is no need to mention the view controller instance to add the navigation item.
-> To display custom button in a view controller in which custom bar button is crated (i.e. in self) :
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:#"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 70, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
controller.navigationItem.rightBarButtonItem = inviteButtonContainer;
}
-> To display custom bar button in a view controller that is about to present
eg. To display a view controller after a cell is selected in table view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DjDetailsViewController *controller = (DjDetailsViewController *)
[self.storyboard instantiateViewControllerWithIdentifier:#"DjDetailsViewController"];
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:#"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 70, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
controller.navigationItem.rightBarButtonItem = inviteButtonContainer;
[self.navigationController pushViewController:controller animated:YES];
}
I didn’t done major. I just used your code and change frame of custom bar button.
I hope this will help you.
You issue is with your invite button initialiser:
UIButton *inviteButton = [UIButton UIButtonTypeCustom];
UIButtonTypeCustom is an enum value not a class method/convenience constructor.
use
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
Hope this helps

how to override the functionality of the navigation bar created by pushViewController

I have a view controller, which is presented using pushViewController. This methods presents the view controller with a navigation bar, with a back button on the left side (this is a cocoa method, created by apple).
I want the back button to do just what it does right now, but I also want to tweak some things with it's animation.
How can I add code which works between the button being pressed and the actual animation and return?
thank you.
I do similar stuff by adding my custom back button like this:
UIImage *backArrow = [UIImage imageNamed:backButtonImage];
UIButton *aBackButton = [UIButton buttonWithType:UIButtonTypeSystem];
CGSize aBackButtonTextSize = [#"Back" sizeWithFont:[UIFont systemFontOfSize:17]];
aBackButton.frame = CGRectMake(0.0, 0.0, aBackButtonTextSize.width + backArrow.size.width, self.navigationController.navigationBar.frame.size.height);
aBackButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
SEL backSelector = NSSelectorFromString(#"backAction");
[aBackButton addTarget:self action:backSelector forControlEvents:UIControlEventTouchUpInside];
[aBackButton setTitle:#"Back" forState:UIControlStateNormal];
[aBackButton setImage:backArrow forState:UIControlStateNormal];
[aBackButton setExclusiveTouch:YES];
aBackButton.titleLabel.font = [UIFont systemFontOfSize:17];
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aBackButton];
UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8;
self.navigationItem.leftBarButtonItems = #[aNegativeSpacer, aLeftBarButtonItem];
And then handle user taps and add my own stuff here:
- (void)backAction {
// My own handlings
}

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
}

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