Setting title of UINavigationBar - objective-c

I have an application which uses a main story board to include a Navigation Controller where the main view is a Table View using a prototype cell content. Each cell in the table view pushes onto a new view which I have created with it's own set of .h .m and .xib files.
The table view navigation bar has its title set through the story board which works fine. However, I am having trouble setting the title for each new view after it gets pushed into view.
I have the following in the viewDidLoad method for each view;
self.title = #"View Title";
Any advice?

it is the right way. it will work.

I'm doing something similar in an app I'm working on. Here's how I'm setting the title:
[[self navigationItem] setTitle:#"My View's Title"];
I thinkt the dot notation equivalent would be something like this:
self.navigationItem.title = #"My View's Title";
Hope that helps.

If you have a tab bar controller, try this:
self.tabBarController.navigationItem.title = #"Title";

Related

"back" text displayed in iOS7 UINavigationBar when view title is long

In my application,I need to show the previous viewController title to current viewController back title.
Its working perfectly in iOS6.
In iOS7,automatically the "back" title displayed other than the previous viewController title.
how to fix the issue in iOS7?
In iOS 7 you will not be allowed to set the back button's title to be any longer than 11 characters.
To avoid changing the title of the view controller, but to change the back button's title, you need to do this:
In the previous view controller (the one that will have the next view controller pushed on top of it) you need to set the backBarButtonItem like so:
/**
* Notifies the view controller that its view was added to a view hierarchy.
*
* #param animated If YES, the view was added to the window using an animation.
*/
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.title = #"My Title Can Be Long";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"ThisIsLimit"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
}
Now, when the next view controller is pushed on top of it, the back button will be whatever title you put in the backBarButtonItem.
Due to low reputation I cannot add a comment so I'm posting an answer while this is not actually an answer.
But,
self.navigationController.navigationBar.topItem.title = #"";
which is written in one of the answers, is equivalent to:
self.title = #"";
try this,
self.navigationController.navigationBar.topItem.title = #"";
iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.
if you want to make short title you can do as below
self.title = #"SOME REALLY LONG NAVIGATION BAR TITLE";
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
label.text=self.navigationItem.title;
label.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=label;

How to specify the left, right, or title View button in XIB

I do not want to create a custom navigation bar.
I am pushing a UIViewController and I want to customize how the navigation bar looks for that UIViewController
In story board, we just specify the segue and a nav bar show up on the screen. We just drag and drop UIBarItem to the left and right.
In XIB, the navigationBar simply doesn't show up.
I can add navigation Item but the one I added is ignored.
I've heard that there used to be an outlet called navigationItem but it's deprecated for reason I do not know.
I can add UINavigationBar, however that would be adding my own custom bar. I want the navBar that's provided by UINavigationController.
The appearance of the Navigation Bar in the storyboard designer is just there to illustrate how your screen will look when you load your ViewController inside a UINavigationController. It doesn't mean that you actually have a navigation controller in your app.
You need to add a UINavigationController to your storyboard (probably as the first scene), and then connect your ViewController to it (as the root view controller).
Then you should be able to set your title in the storyboard designer, and drag bar button items onto the navigation bar.
See also the answer to this question.
You can setup it in code.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Title";
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back"style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItem = "Create button" ;
self.navigationItem.leftBarButtonItem = "Create button" ;
self.navigationItem.titleView = "Create custom title view";
}
I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

How to load a view controller using a segmentControl value change event?

I have three views each represented by a View controller named firstVC, secondVC and thirdVC. In the firstView (firstVC) I have a segmented control with three options first,second and third. When I click on the second segment control I want to load the corresponding view controller which in this case would be secondVC. I am new to View Controllers but I did try various options. I created an outlet and also a value changed event. I event tried pushing a view controller when the value changes but it pulls a blank page as opposed to secondVC.
Please see below for the code.
- (IBAction)chainAction:(id)sender {
UINavigationController *navcon = self.navigationController;
if (chainSegControl.selectedSegmentIndex == 1) {
secondVC *atSecondVCChain = [[secondVC alloc] init];
[navcon pushViewController:atSecondVCChain animated:YES];
}
}
Any help would be greatly appreciated. Thank You.
P.S I am using Xcode 4.2
Instead of using alloc to create a controller, define a named segue in your storyboard (if you don't already have one) that describes a push from firstVC to secondVC and then use the performSegueWithIdentifier:sender: method instead of pushViewController:.

Adding a Navigation Controller to a View based Application adds top margin

I am trying to programmatically add a Navigation Controller to my View based Application. This is the code I am using (this code gets called after a button press in a view controller):
MainMenu *control = [[MainMenu alloc] initWithNibName: #"MainMenu" bundle: nil];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: control];
[self.view addSubview:navControl.view];
[control release];
That works, but this ends up happening:
Notice the odd margin above the Navigation control.... My View controller that I am adding the Navigation Controller to has a gray background which you can see.
Any ideas??
If you have a better way of adding a Navigation Controller to a View based Application I am very open to suggestions!
Thank you in advance!
Thank you both for your response, but unfortunately, wantsFullScreenLayout set to YES or NO in the code didn't have any effect. I was able to push the Navigation Controller up by 20 using this line of code:
self.navigationController.navigationBar.frame = CGRectOffset(self.navigationController.navigationBar.frame, 0.0, -20.0);
but then what happened was that the View Controller did not move up with the Navigation bar and left a gap below the Navigation Bar and the View Controller. What eventually worked was checking the Wants Full Screen checkbox in IB in the MainWindow view controller that is automatically generated when you set up a view based application.
The gap you are seeing is the same height as a status bar. Check the status bar settings in your NIB file.
Chances are you want to make the UINavigationController the root view controller for the window, rather than whichever view controller you have now. That would be the better way to do it.
The reason you're seeing that extra margin at the top is because UINavigationController normally expects that it will be sized to fill the entire screen (except perhaps a tab bar at the bottom, if it's inside a UITabBarController), and therefore expects that the top edge of its view will be under the status bar if the status bar is visible. Therefore, it places its navigation bar 20 pixels below the top of its view to leave space for the status bar, without bothering to check whether its view actually is under the status bar. Interestingly, sometimes a re-layout operation will perform this check, but that's unreliable. What I've found works well in a situation like this is to set the UINavigationController's wantsFullScreenLayout property to NO. Then ti doesn't try to leave room for the status bar, so everything works as expected.
I've been struggling with this same issue this morning. Since setting the wantsFullScreenLayout property doesn't seem to have any effect, I resorted to using a little subclass, which worked fine:
#interface MyNavigationController : UINavigationController
#end
#implementation MyNavigationController
- (BOOL)wantsFullScreenLayout;
{
return NO;
}
#end
Its so simple to remove that gap..
self.navigationBar.view.frame = CGRectMake(0, -20, 320, 480);

UISearchBar Not appearing

I have view controller, into the view i have put a table view, and a search bar into the table's header... the search bar is not showing up, just the empty table view.
Do i need to do something additional? I'm pretty sure its to do with the view outlet of the UIViewController, set to View...
Thanks
For anyone else who may be landing on this question, I had a very similar situation where a UITableViewController with a UISearchBar added to it wasn't displaying. If you find yourself in this situation, double check that you are actually calling:
initWithNibName:#"MyNibName" bundle:nil
to init your view controller instead of the common Table View init of:
initWithStyle:UITableViewStylePlain
I was foolishly adding the search bar to the Nib, and then loading it with the style init (which skips the Nib entirely and loads the table view from scratch)
try setting the tableHeaderView of your tableView to your searchBar.
self.tableView.tableHeaderView = searchBar;
if you are using IB, be sure to connect the outlets so that it gets referenced.
Try this,l it worked for me:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.tableView.tableHeaderView = _searchBar;
[_searchBar becomeFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[_searchBar resignFirstResponder];
self.tableView.tableHeaderView = _searchBar;
}
-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
searchBar.frame = CGRectMake(0,MAX(0,scrollView.contentOffset.y),320,44);
}
If you set your view controller as a part of tabbar controller, besides "Class" property in IB set the "NIB name" property. It takes me several hours to figure it out.
Fixed using a UISearchBar with controller.