UINavigationController通过栈的方式来管理视图,通过push将视图压入栈,pop将视图推出栈。
下面通过简单的示例说明
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; ViewController *vc = [[ViewController alloc] init]; UINavigationController *vcNav = [[UINavigationController alloc] initWithRootViewController:vc]; self.window.rootViewController = vcNav; [self.window makeKeyAndVisible]; return YES; }
ViewController.m
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; /* 导航标题的设置可通过三种方式,且显示优先级如下: self.navigationItem.titleView > self.navigationItem.title > self.title 例如这里我们同时使用前两种方式,则默认显示titleView的内容 */ //导航标题 UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 500, 30)]; labelView.text = @"我是根视图"; labelView.textAlignment = NSTextAlignmentCenter; self.navigationItem.titleView = labelView; self.navigationItem.title = @"我不是根视图"; //左边导航按钮 UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; [leftButton setTitle:@"按钮" forState:UIControlStateNormal]; [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; self.navigationItem.leftBarButtonItem = leftItem; //右边导航按钮 UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; [rightButton setTitle:@"跳转" forState:UIControlStateNormal]; [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [rightButton addTarget:self action:@selector(clickedRightButton) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = rightItem; /* backBarButtonItem的使用 如果AController push到 BController,那么有以下几种情况 1:B设置了leftBarButtonItem,则优先显示B的leftBarButtonItem 2:B没有设置leftBarButtonItem,A设置了backBarButtonItem,则显示A设置的backBarButtonItem 3:B没有设置leftBarButtonItem,A没有设置backBarButtonItem,则系统会拿到A的title显示一个返回按钮 所以B左边按钮的显示优先级如下: B的leftBarButtonItem > A的backBarButtonItem > 系统默认的返回按钮 大家可以先注释掉标号①的那行代码,然后同时注释掉标号①②的代码,来试验一下。 这里注意,backBarButtonItem只能自定义title和image,如下注释掉的方法是无效的。 */ //返回按钮 // UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; // [backButton setTitle:@"返回" forState:UIControlStateNormal]; // [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"我是返回" style:UIBarButtonItemStylePlain target:self action:nil]; self.navigationItem.backBarButtonItem = backItem; //② UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width-40, 20)]; tipsLabel.text = @"我是根视图"; tipsLabel.textAlignment = NSTextAlignmentCenter; tipsLabel.textColor = [UIColor redColor]; [self.view addSubview:tipsLabel]; } - (void)clickedRightButton { ChildViewController *childVC = [[ChildViewController alloc] init]; [self.navigationController pushViewController:childVC animated:YES]; }
ChildViewController.m
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor lightGrayColor]; //导航标题 UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 30)]; labelView.text = @"我是子视图"; labelView.textAlignment = NSTextAlignmentCenter; self.navigationItem.titleView = labelView; //左边导航按钮 UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; [leftButton setTitle:@"返回" forState:UIControlStateNormal]; [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [leftButton addTarget:self action:@selector(clickedLeftButton) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton]; self.navigationItem.leftBarButtonItem = leftItem; //① //右边导航按钮 UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)]; [rightButton setTitle:@"按钮" forState:UIControlStateNormal]; [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = rightItem; UILabel *tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, self.view.frame.size.width-40, 20)]; tipsLabel.text = @"我是子视图"; tipsLabel.textAlignment = NSTextAlignmentCenter; tipsLabel.textColor = [UIColor blueColor]; [self.view addSubview:tipsLabel]; } - (void)clickedLeftButton { [self.navigationController popViewControllerAnimated:YES]; }
再说明下titleView的宽度问题
根视图中将labelView设置了500的宽度,而子视图中将labelView设置了10的宽度,但是显示效果是一样的,下面我们详细对比下这两种有什么不同。
我们可以看到,leftBarButtonItem距离左边距有16个像素,和titleView至少有6像素的距离;rightBarButtonItem同理。
所以我们假设屏幕宽度为ScreenW,标题文字的实际宽度为labelW,左边按钮宽度为leftW,右边按钮宽度为rightW,titleView的最终宽度为titleViewW。
①如果titleViewW < labelW,则titleViewW 为 labelW;
②如果labelW < titleViewW < ScreenW-16*2-6*2-leftW-rightW,则titleViewW 为 titleViewW;
③如果titleViewW > ScreenW-16*2-6*2-leftW-rightW,则titleViewW 为 ScreenW-16*2-6*2-leftW-rightW。
实际开发中可能会遇到标题过长,需要使用省略号“…”显示,如下图所示
此时让标题居中的办法就是将leftW和rightW设置为相等即可;
如果只有左边返回按钮,而没有右边按钮,则放置一个空视图占位,即可方便实现标题居中。