iOS 7 backBarButtonItem hidden - ios

I've an issue with back bar button. It stay hidden no matter that i do like self.navigationItem.hidesBackButton
Here is my code to add back button:
//
- (void)viewDidLoad{
[.....];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"UI_BTN_BACK", nil) style:UIBarButtonItemStylePlain target:nil action:nil];
back.tintColor = [Templates getColor:#"color"];
[[self navigationItem] setBackBarButtonItem:back];
// Parent
[super viewDidLoad];
}
Button stay hidden BUT going back works if if touch on the place where it should be.
Of course it works on iOS6.
Another detail: back button seems to appear when i set UINavigationBar translucent to YES.
Thanks

[self.navigationItem setHidesBackButton:YES];
Check out this.

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

Use this code:
self.navigationItem.backBarButtonItem = nil;
OR
self.navigationItem.leftBarButtonItem = nil;

Related

UIImagePickerController inside UIPopoverController doesn't show Cancel button on iOS7

I'm using UIImagePickerController inside a UIPopoverController to select image just from photo albums. When I launch app on device running iOS 8, the Cancel button on the top right of the pop over appeared normally like this:
But when I launch the app on device running iOS 7, the Cancel button disappeared:
The code I used to show the picker:
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
[pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
pickerController.delegate = self;
_popOver = [[UIPopoverController alloc] initWithContentViewController:pickerController];
_popOver.delegate = self;
pickerController.navigationBar.tintColor = [UIColor redColor];//Cancel button text color
[pickerController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor blackColor]}];// title color
if (isImage) {
[pickerController setMediaTypes:#[(NSString*)kUTTypeImage]];
} else
[pickerController setMediaTypes:#[(NSString*)kUTTypeMovie]];
[_popOver presentPopoverFromRect:CGRectMake(1024/2, 768/2, 1, 1) inView:self.view permittedArrowDirections:0 animated:YES];
What can I do to show that Cancel button on iOS7? My app design doesn't allow user to dismiss the popover by tapping anywhere outside the popover view.
Thank you.
#JozoL Write the Below Code this works
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UINavigationItem *pickerNavBarTopItem;
// add done button to right side of nav bar
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain
target:self
action:#selector(doSomething)];
UINavigationBar *bar = navigationController.navigationBar;
[bar setHidden:NO];
pickerNavBarTopItem = bar.topItem;
pickerNavBarTopItem.rightBarButtonItem = doneButton;
}
-(void)doSomething{
}
For swift2.x you can try below code, Its work for me
pickerController.navigationBar.tintColor = UIColor.blueColor()
In my case, UIImagePickerController was not showing its cancel button
. So I tried adding this line to code:
pickerController.navigationBar.barStyle = UIBarStyleDefault;
And this worked for me.Try setting color of Cancel button like this
pickerController.navigationBar.tintColor = [UIColor blackColor];
Try using this code and let me know if it helps you.

Done button item has a text of Edit

I have a navbar with a rightbarbuttonitem of:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
Though when I compile the app onto a device, the button says edit on it instead of done like it is supposed to. Why is this
I add the button like this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
This is the whole code for the view:
-(void) done {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Edit";
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];
// Do any additional setup after loading the view.
}
I am not using an xib file just pure code and am presenting to the controller from another
Your issue is that you are using the style of a done button which means it will say edit instead of done as it is basically an edit button which doesn't respond to some selectors edit buttons do. To fix this change your code to this:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:#selector(done)];

UINavigationBar UIBarButtonItem works on left but not on right

I have two buttons on my navbar. One on the left and one on the right. The button on the left works but the one on the right does not work. If I swap the buttons, recompile and load the issue persist with the right button regardless of which button is in the right spot of the bar.
Also while the left button works as soon as I try to use the right button the left button stops working too.
How do I make the right button work? Below is the code.
UINavigationBar *NavBar = [[UINavigationBar alloc] init];
[NavBar sizeToFit];
[self.view addSubview:NavBar];
UIBarButtonItem *cmdBack = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdBackClicked:)];
UIBarButtonItem *cmdAddDevice = [[UIBarButtonItem alloc] initWithTitle:#"Add Device"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cmdAddDeviceClicked:)];
UINavigationItem *NavBarItems = [[UINavigationItem alloc] initWithTitle:#"Settings"];
NavBarItems.leftBarButtonItem = cmdBack;
NavBarItems.rightBarButtonItem = cmdAddDevice;
NavBar.items = [NSArray arrayWithObjects:NavBarItems, nil];
The functions for the buttons are these
- (void) cmdBackClicked:(id)sender
{
[self.view removeFromSuperview];
}
- (void) cmdAddDeviceClicked:(id)sender
{
vcAddDevice *ViewAddDevice = [[vcAddDevice alloc] initWithNibName:#"vcAddDevice" bundle:nil];
[ViewAddDevice SetEditDeviceMode:[NSString stringWithFormat:#"No"]];
[self.view addSubview:ViewAddDevice.view];
}
In the .h file I have this.
#import <UIKit/UIKit.h>
#import "vcAddDevice.h"
#interface ViewControllerSettings : UIViewController<vcAddDeviceControllerDelegate, UITableViewDelegate, UITableViewDataSource>
- (IBAction) cmdBackClicked:(id) sender;
- (IBAction) cmdAddDeviceClicked:(id) sender;
#end

How to transition UIToolbar with it's parent view

How can I transition from one UIViewController (with bottom toolbar) to another one (without toolbar), so that while the animation is in progress, the toolbar moves away with the first view, meaning that the toolbar stays in it's initial position of the first view?
I've seen this behaviour in "Things" app.
Just to be clear, I am not looking for solutions such as hiding/showing the toolbar inside viewDidAppear.
So, this is a solution I wasn't very happy about, but at the moment it seems as the only one.
The point is to ignore the built in toolbar property of UINavigationController, create separate UIToolbar and place it inside your view controller.
// Create your bar items
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects: flexibleSpace, nil];
[flexibleSpace release], flexibleSpace = nil;
UIToolbar *customToolbar = [[UIToolbar alloc] init];
[customToolbar sizeToFit];
[customToolbar setFrame:CGRectMake(0.0, self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height - customToolbar.frame.size.height, customToolbar.frame.size.width, customToolbar.frame.size.height)];
[customToolbar setItems:toolbarItems];
[[self view] addSubview:customToolbar];
[customToolbar release], customToolbar = nil;
[toolbarItems release], toolbarItems = nil;
This way the toolbar will slide away with it's view, causing no animation issue such as "white rectangles" or late appearing toolbar when placed in viewDidAppear...
It sounds like you want:
- ( void )viewWillAppear: (BOOL)animated
{
NSArray *items = ... UIBarButtonItem array...;
[ super viewWillAppear: animated ];
[ self setToolbarItems: items animated: animated ];
....
}

How to add a right bar button to a navigation bar in iphone

I want to add a right bar button item to the navigation bar, so that on click, it performs certain a function.
I have created the following code to add the right bar button item, but after it is done, the bar button item is not getting displayed in navigation bar:
-(void)viewDidload{
self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Let assume, you have a UIViewController, like this:
UIViewController *vc = [UIViewController new];
And you added it to navigation controller:
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
In this way rightBarButtonItem will NOT getting displayed:
nc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
BUT this way IT WILL appear:
vc.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"xyz" style:UIBarButtonItemStyleDone target:self action:#selector(xyz)];
Use your own viewcontroller instead of the navigationcontroller to refer navigationItem.
-(void)viewDidLoad
{
[super viewDidLoad];
app.navigationController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(Add:)] autorelease];
}
-(IBAction)Add:(id)sender
{
TAddNewJourney *j=[[TAddNewJourney alloc]init];
[app.navigationController pushViewController:j animated:YES];
[j release];
}
Try the other answers. I posted this answer so that it will work if your viewcontroller does not have a navigation controller which i think is the problem.
Add this code in viewdidload
UIBarButtonItem *chkmanuaaly = [[UIBarButtonItem alloc]initWithTitle:#"Calculate" style:UIBarButtonItemStylePlain target:self action:#selector(nextview:)];
self.navigationItem.rightBarButtonItem = chkmanuaaly;
Most of the answers here address setting the UINavigationBar's rightBarButtonItem from within the new VC after it is shown. My need, which was actually answered by János, is to set the UINavigationItem items from the CALLING UIViewController. Hence, the following code (Thanks, János).
// from within self (aka the calling controller):
UIViewController *c = [[[UIViewController alloc] init...] autorelease];
c.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissModalViewControllerAnimated:)] autorelease];
c.navigationItem.title = #"Title Goes Here";
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:c] autorelease];
nav.navigationBarHidden = FALSE;
[self presentModalViewController:nav animated:YES];
Now, granted, this may seem redundant to János' answer, but I need more rep points in order o mark his response up. ;-)
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
I guess it will be more complete response and it should help in any situation:
-(void)viewDidLoad{
//Set Navigation Bar
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//Set title if needed
UINavigationItem * navTitle = [[UINavigationItem alloc] init];
navTitle.title = #"Title";
//Here you create info button and customize it
UIButton * tempButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
//Add selector to info button
[tempButton addTarget:self action:#selector(infoButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:tempButton];
//In this case your button will be on the right side
navTitle.rightBarButtonItem = infoButton;
//Add NavigationBar on main view
navBar.items = #[navTitle];
[self.view addSubview:navBar];
}
Use the following code:
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addUser)];
self.navigationItem.rightBarButtonItem=add;
[add release];
Hope this helps you out.
Enjoy!
Swift:
let rightButton = UIBarButtonItem(image: UIImage(named: "imageName"),
style: .plain,
target: self,
action: #selector(buttonTapped))
navigationItem.setRightBarButton(rightButton, animated: false)
#objc func buttonTapped(sender: UIBarButtonItem!) {
// Implement action
}
UIButton *dButton=[UIButton buttonWithType:0];
dButton.frame=CGRectMake(50,50,50,50);
[dButton addTarget:self action:#selector(clickdButton:)
forControlEvents:UIControlEventTouchUpInside];
[dButton setImage:[UIImage imageNamed:#"iconnavbar.png"]
forState:UIControlStateNormal];
dButton.adjustsImageWhenHighlighted=NO;
dButton.adjustsImageWhenDisabled=NO;
dButton.tag=0;
dButton.backgroundColor=[UIColor clearColor];
UIBarButtonItem *RightButton=[[[UIBarButtonItem alloc] initWithCustomView:dButton]autorelease];
self.navigationItem.rightBarButtonItem=RightButton;
and then:
-(IBAction)clickdButton:(id)sender{
classexample *tempController=[[classexample alloc] init];
[self.navigationController pushViewController:tempController animated:YES];
[tempController autorelease];
}

Resources