Navigation bar back button text is not changing - ios

I am trying to change the text of the navigation bar back button text. I have tried the solutions offered here but I cant get the text I want on back button. In appdelegate I set these features:
[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:#"e74c3c"]];
[[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:#"e74c3c"]];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[UIFont fontWithName:#"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont fontWithName:#"Futura-CondensedMedium" size:19.0]
}
forState:UIControlStateNormal];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
And in every viewcontroller's viewdidload I set:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];
or
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
Any suggestions?

If I haven't misunderstood you you can do this to set the title of the back button. Suppose there are three view controllers: A, B and C. Now you want the title of the back button to be "MyViewA" when you navigate from A to B then you need to put this code in "viewWillAppear:" of your ViewController A (this will show the title of the back button as "MyViewA" when you are on view controller B).
//viewWillAppear of ViewController A
-(void)viewWillAppear:(BOOL)animated{
//now set title of back button
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"YourTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
}
Likewise if you are navigating from B to C and want to set the back button title for ViewController C, you need to put the above code in "viewWillAppear:" delegate of ViewController B. In short you need to set the title of the next view controller on your current view controller.

You can't change the value of the text in the backbutton. You have to set the button as the left or right barButtonItem (probably left if you want it as back button) like this:
[self.navigationItem setLeftBarButtonItem:newBackButton];
you can then attach an IBAction to the button by setting it when you init your button:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"NewTitle"
style:UIBarButtonItemStyleBordered
target:nil
action:yourBackFunction];
at least that would be a workaround and I have also used it before...

The back button belongs to the previous view controller, not the one currently presented on screen.
To modify the back button you should update it before pushing, on the view controller that initiated the segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Delete back item title in next controller
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"New Title"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
}

Related

UICollectionView Top Bar Doesn't Appear In Simulator

I had a little problem which is my uicollectionviewcontroller top bar doesn't appear in simulator and device. I already set the top bar to translucent navigation bar for uicollectionviewcontroller, then I dragged navigation item to navigation bar and finally I set an image for left bar button items to function as back button. Below is there screenshot.
Top Bar Storyboard:
Simulator Result:
It doesn't look like you have a navigation controller, to add a navigation bar programatically without a navigation controller controlling the view hierarchy you can do the following.
-(void) viewWillAppear:(BOOL)animated {
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];
[self.view addSubview: navBar];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navItem.rightBarButtonItem = doneItem;
navItem.leftBarButtonItem = cancelItem;
navBar.items = [NSArray arrayWithObjects: navItem,nil];
[UIBarButtonItem appearance].tintColor = [UIColor blueColor];
}
To add the navigation controller (which will add the bar), highlight the UICollectionViewController in storyboard, and select Editor->Embed in->Navigation Controller from the menus.

Attempting to load the view of a view controller while it is deallocating - Objective C

Im not sure where this error is coming from, it occurs when I hit back on the navigation controller and go to the previous screen, I am guessing it has to do with something in viewWillAppear but this is all that in it
self.view.backgroundColor = mainColor;
self.topView.backgroundColor = mainColor;
[self.navigationController.navigationBar setBarTintColor:offSetColor];
profileBtn.tintColor = tempColor;
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:18], NSFontAttributeName,tempColor,
NSForegroundColorAttributeName, nil]];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
[imageView setImage:self.homeImage];
self.title = self.titleReal;
Not sure how any of that could cause a problem the second time, when I go back to this screen when it works fine the first time?
Thanks for the help

UIBarButtonItem tintColor stopped working

For some reason I can't use UINavigationBar on my view and I use UIToolBar instead. I was using dummy left button with clear tintColor and title button with black tintColor to properly center the title and hide the dummy left bar button, but it's not working anymore and both the dummy left button and the title button get either the main tintColor or if I disable them, they become gray.
How can I make them ideally disabled as buttons and set the proper tintColor (clear and black)?
- (void)addToolbar
{
CGFloat barHeight = 44.0;
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - barHeight, self.view.bounds.size.height, barHeight)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil action:nil];
// Done button on the right
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
// dummy button on the left to have centered title
UIBarButtonItem *dummyButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
dummyButtonItem.enabled = NO;
dummyButtonItem.tintColor = [UIColor clearColor];
// title button
UIBarButtonItem *titleButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Title text here" style:UIBarButtonItemStylePlain target:nil action:nil];
titleButtonItem.enabled = NO;
titleButtonItem.tintColor = [UIColor blackColor];
// set toolbar items
toolbar.items = #[dummyButtonItem,flexibleSpaceButtonItem, titleButtonItem, flexibleSpaceButtonItem,doneButtonItem];
[self.view addSubview:toolbar];
}
When you specify a tint for a view, the tint is automatically propagated to all subviews in the view’s hierarchy. Because UIWindow inherits from UIView, you can specify a tint color for the entire app by setting the window’s tint property using code like this:
Under application:didFinishLaunchingWithOptions:
window.tintColor = [UIColor purpleColor];
Source - iOS Transitions . Check it out under Using Tint Color section
Based on your feedback I found that by doing this in AppDelegate:
self.window.tintColor = [UIColor colorWithRed:255/255.0 green:59/255.0 blue:48/255.0 alpha:1.0];
[[UITextField appearance] setTintColor:[UIColor colorWithRed:66/255.0 green:107/255.0 blue:242/255.0 alpha:1.0]];
the second line causes my issue. I now need to find out why did I put the second line there :). Thanks for your help.

How to reload Navigation bar

In UITableView I configure self.navigationItem.leftBarButtonItem with a button. Now I want to reload this button to update text on this button, but it's on Navigation bar. I can update UITableView by
[self.tableView reloadData]
but How to update Navigation bar?
Update: I'm NOT init the title of button, but reload it. So any init code (such as self.navigationItem.leftBarButtonItem.title = #"balabala") doesn't work, because it does NOT take effect immediately.
Just change the text on the leftBarButtonItem?
self.navigationItem.leftBarButtonItem.title = #"Back"; // or whatever text you want
Try this
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:#"Title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(OnClick:)];
self.navigationItem.leftBarButtonItem = btnBack;
Hope this helps you..
EDIT :-
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithTitle: #"Title" style: UIBarButtonItemStyleBordered target:self action:#selector(OnClick:)];
[[self navigationItem] setBackBarButtonItem: btnBack];
Did you try setting the text, using
self.navigationItem.leftBarButtonItem.title
You need to use the following statement:
self.navigationItem setLeftBarButtonItem:cancelButton animated:YES];
In other words you need the setLeftBarButtonItem:animated message instead of the standard one. This will reload the button text with animation. Same applies for the back button if that is what you are interested in changing at some point.
#interface Myviewcontroller : UIViewController
{
UIBarButtonItem *barBtnBack;
UIButton *btnQuestion;
}
//ViewdidLoad
btnQuestion = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 25)];
[btnQuestion setBackgroundImage:[UIImage imageNamed:#"question-mark2x"] forState:UIControlStateNormal];
[btnQuestion addTarget:self action:#selector(goBacktomain)
forControlEvents:UIControlEventTouchUpInside];
barBtnBack =[[UIBarButtonItem alloc] initWithCustomView:btnQuestion];
[self.navigationItem setLeftBarButtonItem:barBtnBack];
note- change btnQuestion title when you want to change navigationnar button title
If you want to change the NavigationBar title, try 1 of these:
[self.navigationController.navigationItem setTitle:#"Title"];
[self.navigationItem setTitle:#"Title"];
[self setTitle:#"Title"];
If you wanna change button titles, put this in the same class:
- (void)setLeftBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setLeftBarButtonItem:leftButton];
}
- (void)setRightBarButtonTitle:(NSString *)title action:(SEL)selector
{
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:selector];
[self.navigationItem setRightBarButtonItem:rightButton];
}
Examples to set button title and actions:
// Change button titles and actions any time
[self setLeftBarButtonTitle:#"Back" action:#selector(goBack)];
[self setRightBarButtonTitle:#"Something" action:#selector(doSomething:)];
// Change button titles with no actions
[self setLeftBarButtonTitle:#"Nothing" action:nil];
[self setRightBarButtonTitle:#"Nothing" action:nil];
// Remove buttons from navigation bar
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:nil];
Try this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"your_title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backBtnPressed:)];
self.navigationItem.leftBarButtonItem = backButton;
I solved it in Swift, but if the keyboard is shown, it hides.
navigationController?.loadView()

How to add Bar Button in navigation bar without navigation controller.

I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.
"i don't have navigation controller or need it. i just want to add navigation bar in only one view."
Bellow is my code which i write for adding navigation bar in ViewDidLoad()
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
Thanks in advance....
I used the Interface Builder to make a static tableView in a UITableViewController.
This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:
//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:#"Sign-In"];
//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Zurück" style:UIBarButtonItemStyleDone target:self action:#selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:#"Fertig" style:UIBarButtonItemStylePlain target:self action:#selector(signInDonePressed:)];
//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];
//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];
// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];
// Adding the NavigationBar to the TableView
[self.tableView setTableHeaderView:headerView];
I hope this helps somebody!
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
[bi1 release];
You can add buttons to navigation bar as follows:
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
navBar.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
navBar.leftBarButtonItem = btnCancel;
[btnCancel release];

Resources