您的位置:首页 > 产品设计 > UI/UE

UINavigationController总结

2015-11-23 18:37 411 查看
1.总述:
当我们利用UINavigationController的时候,无非就是利用其进行页面跳转。在这些页面跳转中,
当我们会遇到诸如:设置UINavigationBar中的内容、屏幕支持的方向等困难。

2.对导航控制器中的控制器的介绍:
1)UINavigationBar--导航条
1> ios7之前是不透明的,ios7之后是透明的。
2> navigationBar竖屏下默认高度是44,横屏下高度为32

2) UINavigationItem
UINavigationBar能管理一组UINavigationItem,UINavigationBar也是通过栈的方式管理一
组UINavigationItem,提供push和pop操作item.
每个视图控制器都有一个navigationItem属性,navigationItem中设置的左按钮,右按钮,标题等会
随着控制器的显示也显示到navigationBar上。其属性为:title-->标题、titleView-->标题视图、
leftBarButtonItem,rightBarButtonItem

3) UIBarButtonItem
定义了UINavigationItem上按钮的触发事件,外观等。
- (instancetype)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithImage:(UIImage *)image landscapeImagePhone:(UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action NS_AVAILABLE_IOS(5_0); // landscapeImagePhone will be used for the bar button image when the bar has Compact or Condensed bar metrics.
- (instancetype)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
- (instancetype)initWithCustomView:(UIView *)customView;

3.当一个控制器被push到一个navigationController中的时候,给控制器设置样式时可能会遇到的
困难:
1)设置UINavigationBar(导航条)的背景颜色:
在根控制器中设置--->
self.navigationController.navigationBar.barTintColor = ?
2) 设置导航条为背景图片:
在根控制器中设置--->
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navback"] forBarMetrics:UIBarMetricsDefault];
3)自定义leftBarButtonItem和rightBarButtonItem:
此时我们需要自己定义一个导航控制器去继承UINavigationControlelr,通过重写push方法来设置左右按钮
// 方法一:通过自定义button来进行
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {  // if the controller was pushed is not the first
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[btn setBackgroundImage:[UIImage imageNamed:@"backItem"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"backItem"] forState:UIControlStateHighlighted];
btn.size = btn.currentBackgroundImage.size;
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
}
[super pushViewController:viewController animated:animated];
}

- (void)back
{
[self popViewControllerAnimated:YES];
}

// 方法二: 通过定义UIBarButtonItem来进行
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonItemTrash) target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;

4) 当需要设置导航控制器不随屏幕旋转而旋转的时候,直接在根控制器中设置其是否支持旋转。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: