首页 > 代码库 > iOS开发-UI (六)Navigation

iOS开发-UI (六)Navigation

知识点:

1.UINavigationController

2.UINavigationItem

3.UINavigationBar

4.UINavigationController视图切换

 

========================

UINavigationController

    1.什么是导航控制器

作用:管理视图控制器

 

    2.UINavigationController对象创建

      1)初始化方式

- (id)initWithRootViewController:(UIViewController *)rootViewController

 

      2)UINavigationController组成:

(1)navigationBar(高度44)

(2)customContent  — 有自定义的ViewController提供

(3)navigationToolbar(高度44)

 

 

    3.通过UINavigationController对象切换视图

      1)将视图控制器压入导航控制器的栈容器中

- (void)pushViewController:(UIViewController *)viewController 

  animated:(BOOL)animated

      2)将视图控制器从导航控制器中弹出

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

  

========================

UINavigationItem

 

UINavigationItem包含了:

(1)backBarButtonItem(由上一级ctl的属性决定)

(2)title/titleView(当前ctl)

(3)rightBarButtonItem(当前ctl)

(4)leftBarButtonItem(当前ctl)

 

 

    1.UINavigationItem和UIViewController关系

navigationItem是UIViewController的一个属性

这个属性是为UINavigationController服务的

 

 

    2.创建UIBarButtonItem

  1)创建系统自带的UIBarButtonSystemItem

      - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem 

      target:(id)target 

  action:(SEL)action;

 

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(btnAction)];

      2)文字UIBarButtonItem的创建方式

- (id)initWithTitle:(NSString *)title

      style:(UIBarButtonItemStyle)style

    target:(id)target

    action:(SEL)action

 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem new];

 

      3)图片UIBarButtonItem的创建方式

- (id)initWithImage:(UIImage *)image 

      style:(UIBarButtonItemStyle)style

    target:(id)target

    action:(SEL)action

 

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"refresh_30"] style:UIBarButtonItemStylePlain target:nil action:nil];

 

      3)如何将UIBarButtonItem加入到导航的左边和右边

@property (nonatomic, retain) UIBarButtonItem *leftBarButtonItem

@property (nonatomic, retain) UIBarButtonItem *rightBarButtonItem

@property (nonatomic, retain) UIBarButtonItem *backBarButtonItem

 

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:b];

 

    3.定制UIBarButtonItem

 

    4.定制导航中间的titleView

self.navigationItem.title = @"控制器2";

 

    5.定制backBarButtonItem

      注意:设置当前控制器的backBarButtonItem需要在下一个控制器中才能显示

 

 

========================

UINavigationBar

    1.如何往UINavigationBar贴图

      设置背景图片

      - (void)setBackgroundImage:(UIImage *)backgroundImage

    forBarMetrics:(UIBarMetrics)barMetrics

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsDefault];

 

    2.如何设置UINavigationBar设置颜色

      @property (nonatomic, retain) UIColor *tintColor

      @property (nonatomic, retain) UIColor *barTintColor

 

    3.如何设置透明颜色

      1)设置UINavigationBar的样式

      @property (nonatomic, assign) UIBarStyle barStyle

   self.navigationController.navigationBar.barStyle= UIBarStyleBlack;

 

      2)是否透明

@property (nonatomic, assign, getter=isTranslucent) BOOL translucent

 

//获取管理当前视图控制器的导航控制器(如果这个视图控制器没有受到导航控制器管理,此方法会返回空指针)

    //设置导航栏为不透明,坐标点会自动下移64个单位

    self.navigationController.navigationBar.translucent = NO;

 

      3)改变导航栏的颜色

@property(nonatomic,retain) UIColor *barTintColor 

 

//设置背景色

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];

 

    4.如何隐藏UINavigationBar

      1)不带动画隐藏

      @property (nonatomic, getter=isNavigationBarHidden) BOOL  navigationBarHidden

 

//显示

    self.navigationController.navigationBarHidden = NO;

 

      2)带动画隐藏

- (void) setNavigationBarHidden:(BOOL)navigationBarHidden 

      animated:(BOOL)animated

      [self.navigationController setNavigationBarHidden:YES animated:YES];

 

========================

UINavigationController视图切换

    1.获取导航控制器中的视图数组

@property (nonatomic, copy) NSArray *viewControllers

 

    2.将视图控制器压入导航控制器的栈容器中

- (void)pushViewController:(UIViewController *)viewController 

  animated:(BOOL)animated

 

//视图控制器入栈

    [self.navigationController pushViewController:ctlA animated:YES];

 

    3.将视图控制器从导航控制器中弹出

- (UIViewController *)popViewControllerAnimated:(BOOL)animated

 

    4.切换至指定的视图控制器(该控制器必须在当前导航控制器中的栈中)

- (NSArray *)popToViewController:(UIViewController *)viewController

                                animated:(BOOL)animated

    5.回到根视图控制器

     - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

 

iOS开发-UI (六)Navigation