UINavigationController、UINavigationBa的详解和导航控制栏的定制

来源:互联网 发布:windows bea 000402 编辑:程序博客网 时间:2024/06/05 10:37

第一.UINavigationController的基本概念

UINavigationController是用于构建分层应用程序的主要工具,管理者多个内容视图的换入和换出,自身提供了切换的动画效果.

第二.基本样式

主要三部分:

1.导航栏:NavigationBar,在最上面,主要负责视图的弹出和控制主视图,高度是44px/32px(横过来),上面的logo是20*20px

2.内容视图,中间部分,用于显示内容

3.工具栏(UIToolBar),默认是隐藏的,用户可以自己添加工具栏,高度是44px/32px(横过来)

第三、示例代码:

 RootViewController *rootViewController=[[[RootViewController alloc] init] autorelease];    NSLog(@"the first controller is %@",rootViewController.navigationController);    UINavigationController *controller=[[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];    NSLog(@"the end controller is %@",rootViewController.navigationController);

输出结果是:

2013-09-10 19:21:13.236 simpleNavigation[1841:c07] the first controller is (null)

2013-09-10 19:21:13.239 simpleNavigation[1841:c07] the end controller is <UINavigationController: 0x892eb70>

分析:
在执行,下面语句之前
 UINavigationController *controller=[[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];


rootViewController的navigatoncontroller为空,在执行了该语句之后navigationcontroller就有值了

    [self.navigationControllerpushViewController:secondVCanimated:YES];就可以实现跳转了

        [self.navigationControllersetToolbarHidden:NOanimated:YES];  //设置Toolbar是否显示

         [self.navigationControllersetNavigationBarHidden:NO]; //设置导航栏是否显示

第三UINavagitionController的代理,通过设置代理监听视图控制器的切换

    self.navigationController.delegate=self;

// Called when the navigation controller shows a new top view controller via a push, pop or setting of the view controller stack.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;


第四.常用方法

- (id)initWithRootViewController:(UIViewController *)rootViewController;//初始化设置跟视图控制器

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;//将新的视图压栈

- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;//出栈到指定的视图控制器

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;//出栈到跟视图控制器

第五.常用属性:

@property(nonatomic,readonly,retain)UIViewController *topViewController;//栈顶的控制器

@property(nonatomic,readonly,retain)UIViewController *visibleViewController;//当前显示的控制器

@property(nonatomic,copy)NSArray *viewControllers; // 栈中包含的所有控制器

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animatedNS_AVAILABLE_IOS(3_0);// If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.

@property(nonatomic,getter=isNavigationBarHidden)BOOL navigationBarHidden;

//navigationBar默认隐藏

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animatedNS_AVAILABLE_IOS(3_0);//设置toobar的状态

@property(nonatomic,readonly) UIToolbar *toolbarNS_AVAILABLE_IOS(3_0);//返回toolbar

UINavigationBar详解

第一、基本概念

一个导航控制器一般应包含以下四个对象:UINavigationController、UINavigationBar、UIViewController、UINavigationItem,其中UINavigationItem存放在UINavigationBar上,一个导航控制器有多个UIViewController,一个视图控制器只有一个UINavigationBar,一个视图控制器对应一个UINavigationItem。

第二、定制导航栏

定制标题视图:

通过NavigatioinItem的titleView属性定制标题视图,titleView属性是一个视图类,可以添加一个UIView的实例,也可以添加UIView的子类

leftBarButtonItem和rightBarButtonItem属性石一个UIBarButtonItem的实例,因此,可以通过初始化UIBarButtonItem实例,设置导航栏的左、右栏目项。

UIBarButtonItem类提供了四个常用的方法

1.    - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;
 实例方法:

    UIBarButtonItem *leftItem=[[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCameratarget:selfaction:@selector(study)]autorelease];

    self.navigationItem.leftBarButtonItem=leftItem;  //正确写法

    self.navigationController.navigationItem.leftBarButtonItem=leftItem; //错误写法

分析原因:

一个导航控制器控制着若干个视图控制器,但仅有一个NavigationBar和一个ToolBar

NavigationBar中的“按钮”是一个UINavigaionItem,UINavigationItem不是有UINavigatonController控制,也不是有UINavigationBar来控制,而是由当前的视图控制器来控制

2. - (id)initWithCustomView:(UIView *)customView;

   UIButton *button2=[UIButtonbuttonWithType:UIButtonTypeRoundedRect];

   [button2 setFrame:CGRectMake(50, 50, 50, 50)];

    [button2 setTitle:@"click"forState:UIControlStateNormal];

    [button2 addTarget:selfaction:@selector(clicked)forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightButtonItem=[[[UIBarButtonItemallocinitWithCustomView:button2]autorelease];

    self.navigationItem.rightBarButtonItem=rightButtonItem;

3.navigationItem的titleView属性可以用来设置中间的title

    UIView *titleView=[[[UIViewallocinitWithFrame:CGRectMake(0,030,30)] autorelease];

    titleView.backgroundColor=[UIColorredColor];

    self.navigationItem.titleView=titleView;

4.- (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

    UIBarButtonItem *rightButtonItem=[[UIBarButtonItemallocinitWithTitle:@"自定" style:UIBarButtonItemStylePlain target:self action:nil];

   self.navigationItem.rightBarButtonItem=rightButtonItem;

    [rightButtonItem release];

5. 实现对leftBarButton的定制

  UIBarButtonItem *leftItem=[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(clickedleft:)]autorelease];    self.navigationItem.leftBarButtonItem=leftItem;

6.实现对中间view的定制:

    UIView *titleView=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)] autorelease];    titleView.backgroundColor=[UIColor blueColor];    self.navigationItem.titleView=titleView;

7.实现对右侧view的定制:

UIButton *button=[[[UIButton alloc]initWithFrame:CGRectMake(5, 5, 30, 30)] autorelease];    [button addTarget:self action:@selector(clickedRight) forControlEvents:UIControlEventTouchUpInside];    [button setBackgroundColor:[UIColor yellowColor]];    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc] initWithCustomView:button];    self.navigationItem.rightBarButtonItem=rightItem;

第三.自定义导航栏背景

导航栏的背景颜色可以通过tinColor来设置,如果要设置背景,可以通过drawRect方法来实现,方法实现如下所示:

@implementation UINavigationBar (SetBackground)

-(void)drawRect:(CGRect)rect

{

    NSLog(@"the rect has been drawed");

    UIImage *image=[UIImageimageNamed:@"button_up"];

    [image drawInRect:rect];

}

@end

5.0以上的SDK提供了设置背景图片的方法.该方法sample如下所示:

    UINavigationController *controller=[[[UINavigationControllerallocinitWithRootViewController:rootViewController]autorelease];

    [controller.navigationBar  setBackgroundImage:[UIImage imageNamed:@"button_up"]forBarMetrics:UIBarMetricsDefault];