Creating a UINavigationController programmatically
Creating a UINavigationController programmatically is trivial. Assuming that you’ve already created a class called RootViewController that subclasses UIViewController, you would use something like this, typically in your AppDelegate file:Basically, we just create an instance of root controller and then we use it to create the navigation controller. Finally, we need to add the navigation controller’s view to the main window.
Adding a title to a navigation controller
Actually you don’t add a title directly to the navigation controller, instead you add it to the view controller that’s being displayed:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My View Controller";
}
Hiding the Navigation Bar
Either in your view controller or where you initiated your navigation controller:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My View Controller";
// hide the navigation bar
// use setNavigationBarHidden:animated: if you need animation
self.navigationController.navigationBarHidden = YES;
}
Showing the navigation toolbar
Either in your view controller or where you initiated your navigation controller:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My View Controller";
// hide the navigation bar
// use setToolbarHidden:animated: if you need animation
self.navigationController.toolbarHidden = NO;
}
Changing the Navigation Bar background color
Either in your view controller or where you initiated your navigation controller:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My View Controller";
self.navigationController.navigationBar.tintColor = [UIColor
colorWithRed:102.0/255
green:52.0/255
blue:133.0/255
alpha:1];
}
Changing the Navigation Bar style
Default style is UIBarStyleDefault, but you can use UIBarStyleBlack and eventually set the translucent propriety to YES.Using a Navigation Bar background image
To use a background image for you navigation bar, you need to subclass UINavigationBar and declare your own drawRect method. You can add this code on its own file or at the top of your application delegate (after the import declarations):Changing Navigation Bar back button text
Here’s how to change the text of the back button:- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My View Controller";
self.navigationController.navigationBar.backItem.title = @"Custom";
}
Adding items to the right of your Navigation Bar
In your view controller:Adding items to the Navigation Toolbar
To add items to your toolbar:Note that the items are not properly aligned. To align them, use flexible space:
No comments:
Post a Comment