how to show back button on navigation bar? - ios

I was practicing an application with navigation bar. I used UIBarButton to go to the next screen from the main screen.
UIButton *moveLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[moveLeft setTitle:#"Next Screen" forState:UIControlStateNormal];
moveLeft.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0f];
[moveLeft.layer setCornerRadius:4.0f];
[moveLeft.layer setBorderWidth:1.0f];
[moveLeft.layer setBorderColor: [[UIColor redColor] CGColor]];
moveLeft.frame=CGRectMake(200, 100.0, 90.0, 30.0);
[moveLeft setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[moveLeft addTarget:self action:#selector(moveToNext) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barbutton= [[UIBarButtonItem alloc] initWithCustomView:moveLeft];
self.navigationItem.rightBarButtonItem = barbutton;
But now when the second screen appears there is no navigation bar.
Here are the screen shots of the app
i tried UIBarButton for back also but still navigation bar is not coming on the second screen.
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
My question how do i show the back button the second screen?? i am not making any custom button but the default is also not coming automatically.
Please guide me.

From your comments, you are presenting the next view controller, instead of this
[self presentViewController:secondScreen animated:YES completion:nil];
Do like this
[self.navigationController pushViewController:secondScreen animated:YES]
Suggestion
Instead of adding subView to the window, you should set the rootViewController of the window which is the recommended way. So change
[self.window addSubview:[navControll view]];
To
self.window.rootViewController = navControll;

No one answered the question correctly, I met the same question, and I have solve it by myself. My solution is delete the navigation controller before the child view controller , than control drag between the tableview controller to the child view controller, select the "show"

You have to use UINavigationController. It will be root view controller for your app. Put mainVC into it and then just pushViewController second vc.
You can access navigation controller via navigationController method of any vc(mainVC in your case).

Simply u can add custom back button to navigationBar
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

Related

UIBarButtonItem to go back is not showing up?

I have no idea why the back button is not being shown after i press a cell on a table. Now i read that on the parent controller (where you click on the cell to move on to the next view) that you should insert this code:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
self.navigationController.navigationBar.tintColor = navBarItems;
And this is how the setup is done:
So my question is why is the back button item not showing up when i click on a cell?
Note:
The funny thing is that i am using a UIPageView where the back button is showing where i use the same code as i did in the parent view controller. So what am i missing? AND i know for sure that is was working on the previous IOS versions. This happened after upgrading maybe that will help you guys a bit?
Thank you
The back button is not being shown because, in the current UINavigationControllerthere is only the cell detail screen so it has nothing to go back to.
I think that you should put that UINavigationController in front of the UIViewController that holds your UITableView
Try This
UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aBtn setImage:[UIImage imageNamed:#"YourImageName"] forState:UIControlStateNormal];
[aBtn setFrame:CGRectMake(0, 0,50, 64)];
[aBtn.titleLabel setFont:[UIFont systemFontOfSize:18.0]];
aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
aBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
aBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[aBtn setTitleColor:[UIColor colorWithRed:1/255.0 green:65/255.0 blue:96/255.0 alpha:1.0] forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarBtn = [[UIBarButtonItem alloc]initWithCustomView:aBtn];
self.navigationItem.leftBarButtonItem = aBarBtn;
-(void)popViewController
{
[self.navigationController popViewControllerAnimated:YES];
}

Reloading navigation bar

How can I reload/reassign navigation bar items? I use some libraries that change navigation bar and sometimes I have a bag in which all navigation items disappear. I have reassigned right items in viewWillAppear like:
UIButton *actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
actionButton.frame = CGRectMake(270, 0, 50, 50);
actionButton.adjustsImageWhenHighlighted = YES;
[actionButton setImage:[UIImage imageNamed:#"share.png"] forState:UIControlStateNormal];
[actionButton setImage:[UIImage imageNamed:#"share-active.png"] forState:UIControlStateHighlighted];
[actionButton addTarget:self action:#selector(presentActivity) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *actionBarButton = [[UIBarButtonItem alloc]initWithCustomView:actionButton];
actionBarButton.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = actionBarButton;
But it does not work and sometimes I do not have any navigation items.
UIBarButtonItem
is not child of UIButton.
here is not known methods like setImage:forState: etc.
create and custon an UITabBarButton like this
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack)];
newBackButton.image = [UIImage imageNamed:#"back"];
self.navigationItem.leftBarButtonItem=newBackButton;
also verify if method with this code will be called when viewDidLoad
Check out this like https://developer.apple.com/library/ios/documentation/uikit/reference/uinavigationbar_class/Reference/UINavigationBar.html the apple dev is awesome.
This is another good link
AppCoda has a lot of great tutorials this one has everything you need to know about Nav Bars
http://www.appcoda.com/customize-navigation-status-bar-ios-7/
and you could also use perform segue with identifier method and to send a string or something to a new view controller and then when you see that string is equal to what you want use an if statement in your viewWillAppear method.

Modal View Controllers - UIBarButtonItems Do Not Persist

I present modal view controllers, packaged in a UINavigationController.
When they first appear, the bar button items are alright.
However, if I click a button and a new view controller is pushed to the UINavigationController, and then I use the back button of that view controller, and it gets popped back to the original modal view controller, the buttons do not show up, (but they work when I click them -> just can't see them)
I can't seem to figure out why this occurs.
Here's some code.
Oh, and by the way, I customize the navigation controller's navigation bar's background. I have a feeling this may be causing this visual interference, but I don't know what to change to get it right.
In GGMainViewController:
GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];
- (void)setupModalNavigationBar:(UINavigationController *)nav {
// unnecessary code removed for a quicker read
// basically navigation bar has a background gradient as well as a bottom border
[nav.navigationBar.layer addSublayer:bottomBorder];
[nav.navigationBar.layer addSublayer:gradient];
}
In GGSelectTeamsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
title = [[UILabel alloc] init];
title.text = someText;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
self.navigationItem.titleView = title;
[title sizeToFit];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
backButton.frame = someFrame;
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
selectButton.frame = someFrame;
[selectButton addTarget:self action:#selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}
Since the navigation items are set in the viewDidLoad method, why can't I see them (but they are still functional) when I return to it from another view controller?
That seems like an over-complicated way of doing this.
Why don't you create your own button and then use the setRightBarButtonItems:items: method from the UINavigationBar to set the buttons you want?
I'm guessing they disappear because you're adding them to the layer of the UINavigationBar.
Still, if you want to keep this method with the layers, you may want to add them in the viewWillAppear method, but you may need call the removeFromSuperlayer to make sure you don't add stuff more than once.
Hope this helps.
The problem was:
the gradients for the navigation bar (done by code)
The solution was:
use Sketch to create an image of the gradients and use the appearance proxy to set the image as a background image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];

Changing UINavigationBar items when traversing the navigation hierarchy

I am trying to customize the items in the UINavigationBar using the following code:
CGSize size = [UIImage imageNamed:kGlobalNavigationBarButtonBackOrange].size;
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
[backButton setImage:[UIImage imageNamed:kGlobalNavigationBarButtonBackOrange] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:kGlobalNavigationBarButtonBackPressedOrange] forState:UIControlStateHighlighted];
[backButton addTarget:self action:#selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Detail"];
[item setLeftBarButtonItem:barButton];
[item setTitleView:self.navigationItem.titleView];
[self.navigationBar pushNavigationItem:item animated:YES];
[self.navigationController pushViewController:vc animated:YES];
However when I run this, the Navigation Bar does not look any different.
What you're trying to do should be done from within your vc view controller. The whole point is that when a new view controller is pushed onto the stack, it is asked for its navigation item and that's used to update the navigation bar.
In - viewDidLoad of your view controller, create the bar button and set it as self. navigationItem. leftBarButtonItem.
You don't need to create a new navigation item or push any nav items onto the nav bar. Let the view controller framework do this for you.

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.

Resources