1.在iOS7.0以上版本中, 如果UINavigationController作为UIWindow的根控制器, 顶部的导航栏会包含状态栏, 并且UINavigationController包含的子视图默认左上角与状态栏左上角对齐, 需要将translucent设置为NO才正常.
2.加入的子控制器, 都包含在childViewControllers属性中, 可以作为只读属性来查, 但是最好不要去更改它, 请用UINavigationController的接口去更改子视图
3.navigationBar是顶部的导航栏, 但是如果想要通过navigationBar.frame来获取导航栏的区域, 是不可行的, 通常使用硬编码来获取这个值:
- 纵向模式为44像素;
- 横向模式为32像素;
- 提示模式为74像素(横向和纵向);
4.导航栏的左右键定制时, 建议使用[initWithCustomView:]方法, [initWithBarButtonSystemItem: target: action:]等方法难以定制按钮的外形
5.使用[pushViewController: animated:]等方法弹出或加入视图时, 可以将animated参数设置为NO, 然后在navigationController.view.layer上建立自定义动画
创建导航栏
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController* controller = [[ViewController alloc] init];
UINavigationController* naviController = [[UINavigationController alloc] initWithRootViewController:controller];
naviController.navigationBar.translucent = YES;
naviController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, [UIFont systemFontOfSize:20.0], NSFontAttributeName, nil]; // 设置字体
[naviController.navigationBar setBackgroundImage:[UIImage imageNamed:@"Ingredients/bg003.png"] forBarMetrics:UIBarMetricsDefault]; // 设置背景
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = naviController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
定制导航栏
- (void)viewDidLoad
{
[super viewDidLoad];
// 定制右键
UIButton* rtButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 80.0, 20.0)];
[rtButton setBackgroundColor:[UIColor grayColor]];
[rtButton setTitle:@"普通" forState:UIControlStateNormal];
[rtButton setTitle:@"高亮" forState:UIControlStateHighlighted];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rtButton];
}
加入或弹出
- (void)popToPrev:(UIButton*)button
{
CATransition* transition = [CATransition animation];
transition.duration = 1.0;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = @"cube";
transition.subtype = kCATransitionFromLeft;
transition.delegate = self;
[self.navigationController.view.layer addAnimation:transition forKey:@"animationxxxx"];
[self.navigationController popViewControllerAnimated:NO];
}
- (void)popToSome:(UIButton*)button
{
UIViewController* controller = [self.navigationController.childViewControllers objectAtIndex:0];
[self.navigationController popToViewController:controller animated:YES];
}
- (void)popToRoof:(UIButton*)button
{
[self.navigationController popToRootViewControllerAnimated:YES];
}