How to create Navigation bar with back button on it? - ios

I am very new to iOS App Development.
In my app I am trying to navigate from second view to first view using navigation bar back button, but while drag and drop the navigation bar in the view I am just able to get the bar with title.
I am really worried and I am not sure what I am doing wrong? I am using Xcode 4.3. and storyboard to design the view.
What i am getting now
Looking for
Thanks for your help guys

Xcode handles this for you.
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
Here is one way to do it using code.

if you want to get a plainButton in the navigation like the iphone`s backButton (only one view), you must add image, i think there is no other way.Of course , if you have 2 or more viewcontrollers , you will use navigationItem.backBarButtonItem without add image .But the first viewcontroller do not have the backButton.
the code like this:
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];

Related

How to make a navigation back button just the complete same style with system?

I tried to make a navigation back button which has the same complete style as system, but no matter how I adjust it still had a little difference.
system
custom
You can see no matter the arrow or the word (返回 BACK) both have some difference.
Here is the code I tried and I got the arrow image from here.
UIImage *sourceImage = [UIImage imageNamed:#"nav-back-btn"];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypePlain];
[backBtn setImage:sourceImage forState:UIControlStateNormal];
[backBtn setTitle:#" 返回" forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goPreviosPage) forControlEvents:UIControlEventTouchUpInside];
backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
backBtn.transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(-8, 0), 1.1, 1.1);
Use the default UINavigationcontroller strategy.
Add a navigation controller and its view controllers.
Give both view controller navigation title. self.navigationItem.title in view did load.
Just pushing the navigation controller will default have a back button and the title of the previous view controller to it with same system stle.
Do not set any custom leftbarbutton item as this may overide the UINAvigationcontroller to show the default back button.
Hope this help.

Custom navigation bar not showing

Controller 1 displays an array of UIImages, Controller 2 manages the paging of those images once selected, and Controller 3 just displays the images for the pages. When I tap on a cell and load a photo into the photoViewController, the paging of images works fine, but my custom navigation bar is overridden with the default bar and I am unable to add any custom buttons to it or change any of its properties in code. The only thing I found that I am able to do is hide it. If I try to change the title of the back button or add my own title to the navigation bar nothing changes. Please help with anything you can.
i think you should try like this
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
and function could be
- (UIBarButtonItem *)leftMenuBarButtonItem {
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 30)]; [backButton setImage:[UIImage imageNamed:#"bar_btn_bg.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(leftDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
if you fall in between let me know

Issue With My Custom Navigation Bar Back Button Xcode

I'm working in Xcode 4.3.2 using storyboards. I've created a segue between two views by clicking on the button I want to create a segue and control+click+drag a connection to the next view controller. Of course this creates an automatic/default back button in the navigation bar in the next view controller. What I want to do is customize the look of this back button. SO, I looked around online and found this code:
self.navigationItem.hidesBackButton = YES;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 4, 40, 40)];
[button setImage:[UIImage imageNamed:#"homeButton.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = back;
It works great to change the LOOK of the button, but the problem now is that when I click that button, Xcode throws an error: "unrecognized error sent to instance..."
Can anyone help me to figure out how to now add the proper functionality to my custom back button? Thanks.
Have you implemented the method - (void)backAction?
To make the back button behave as such with a navigation controller, A possible implementation could look like:
- (void)backAction {
[self.navigationController popViewControllerAnimated:YES];
}
This may need to be tweaked depending on your app setup and what you want it to do when the button is pressed but it should be a good starting point

iOS Custom Back Button without calling method in every View Controller

I would like a more elegant solution to changing the back button for an Application's Navigation Bar. All the answers I've seen only talk about customizing a button and then adding at least one line of code to each View Controller for the new button to display.
I have a category where I modify the Navigation Bar's background and Title. It's a category of UINavigationBar. The category calls willMoveToWindow: to call methods each time a new view is pushed on the Navigation Controller.
So here's how the code to create a new button would look:
- (void)applyCustomButton{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
[backButton setImage:[UIImage imageNamed:#"arrow-left"] forState:UIControlStateNormal];
[backButton setShowsTouchWhenHighlighted:TRUE];
[backButton addTarget:self action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
}
The problem here is that self.navigationItem won't work here. I don't want to bore you with all the things I tried categorizing, but that property only works on View Controllers (I think)?
You don't have to give me a solution for the code above, maybe you have a better way of customizing the Back Button on the Navigation Bar without having to write code on every View Controller Class I have. I just hate repetition because maintaining code like that sucks.
Thanks in advance.

How do I specify an action for a custom Back button in a standalone UINavigationBar?

I have a custom UINavigationBar on a screen, i.e., no navigation controller, as defined below which contains a Back button w/ the title "Media" and the action "mediaViewComplete". However, the mediaViewComplete method is not being called when the button is tapped. How do you specify an action for a Back button?
self.navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
navigationBar.barStyle=UIBarStyleBlackTranslucent;
UINavigationItem *navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *backButton=[[UIBarButtonItem alloc]initWithTitle:#"Media" style:UIBarButtonItemStylePlain target:self action:#selector(mediaViewComplete)];
navigationItem.backBarButtonItem=backButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];
navigationItem=[[UINavigationItem alloc]init];
UIBarButtonItem *editButton=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editCategorization)];
navigationItem.rightBarButtonItem=editButton;
[self.navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[editButton release];
[backButton release];
[navigationItem release];
The problem is that you are using:
navigationItem.backBarButtonItem = backbutton;
The backBarButtonItem has it's own event that is not overridden by your action: option. In fact, Apple guidelines state that you should use "nil" as you action for backBarButtonItem. The backBarButtonItem is not owned by the current view controller.
If you are unconcerned about the arrow shape of the button, you should use leftBarButtonItem instead. If you want to create fully custom back buttons with the arrow shape, you'll have to do some custom magic. Luckily, most of the work has been done for you:
http://idevrecipes.com/2011/01/12/how-do-iphone-apps-instagramreederdailybooth-implement-custom-navigationbar-with-variable-width-back-buttons/
Download this project and look at the results. It even contains the images you need to retain an arrow shaped custom back button. It is more work, but it's much less hack.

Resources