UINavigationController Customization Tutorial

本文档提供了 UINavigationController 的详细自定义教程,包括程序化创建导航控制器、为视图控制器添加标题、隐藏导航栏、显示导航工具栏、更改导航栏背景颜色及样式等操作方法。

http://cocoadevblog.com/uinavigationcontroller-customization-tutorial

 

 

In this tutorial I’ll go through various tasks that are usually performed with a navigation controller. Feel free to jump into a particular section if you’re looking for something specific, or leave a comment if you feel like something is missing.

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:

	- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    	

		// rootViewController can't be an instance of UITabBarController
		// remember to include RootViewController in your class!
		RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

		UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

		[self.window addSubview:navigationController.view];

		// Don't bother releasing navigationController as it needs to be around all the time

	    [self.window makeKeyAndVisible];

	    return YES;
	}
	

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.

	- (void)viewDidLoad {
	    [super viewDidLoad];

		self.title = @"My View Controller";

		self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
	}

	- (void)viewDidLoad {
	    [super viewDidLoad];

		self.title = @"My View Controller";

		self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
		self.navigationController.navigationBar.translucent = 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):

	@implementation UINavigationBar (UINavigationBarCategory)

	- (void)drawRect:(CGRect)rect
	{
		UIImage *image = [UIImage imageNamed:@"bg.png"];
	
		[image drawInRect:rect];
	}
	
	@end

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:

	- (void)viewDidLoad {
	    [super viewDidLoad];

		self.title = @"My View Controller";

		UIBarButtonItem *item = [[UIBarButtonItem alloc] 
								 initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
								 target:self 
								 action:@selector(doSomething)];
		self.navigationItem.rightBarButtonItem = item;

		[item release];
	}

Adding items to the Navigation Toolbar

To add items to your toolbar:

	- (void)viewDidLoad {
	    [super viewDidLoad];

		self.title = @"My View Controller";

		// Show toolbar
		self.navigationController.toolbarHidden = NO;

		UIBarButtonItem *item1 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
								  target:self 
								  action:nil];

		UIBarButtonItem *item2 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
								  target:self 
								  action:nil];


		UIBarButtonItem *item3 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
								  target:self 
								  action:nil];

		NSArray *items = [NSArray arrayWithObjects:item1, item2, item3, nil];

		self.toolbarItems = items;

		[item1 release];
		[item2 release];
		[item3 release];
	}

Note that the items are not properly aligned. To align them, use flexible space:

	- (void)viewDidLoad {
	    [super viewDidLoad];

		self.title = @"My View Controller";

		// Show toolbar
		self.navigationController.toolbarHidden = NO;

		UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] 
										  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
										  target:nil 
										  action:nil];

		UIBarButtonItem *item1 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
								  target:self 
								  action:nil];

		UIBarButtonItem *item2 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
								  target:self 
								  action:nil];


		UIBarButtonItem *item3 = [[UIBarButtonItem alloc] 
							  	  initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
								  target:self 
								  action:nil];

		NSArray *items = [NSArray arrayWithObjects:item1, flexibleSpace, item2, flexibleSpace, item3, nil];

		self.toolbarItems = items;

		[item1 release];
		[item2 release];
		[item3 release];
	}

Conclusion

Take a look at the UINavigationController documentation to find out more about it.

You should subscribe to the RSS Feed Here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值