系统UINavigationController使用相关参考

  闲来无事便在网上google&baidu了一番UINavigationController的相关文章,然后又看了下官方文档;看看更新到iOS7之后UINavigationController的是否有变化,同时也再温故下UINavigationController的使用0.0 平时都是用的自定义的导航栏别到时连系统自己的都不会用了-。-
  这里不介绍UINavigationController的使用,其实这个大部分情况下也只是用于页面的跳转就好了;现在我就记录一些平时没怎么注意到的和还蛮重要的属性,加之理一下层次关系;主要作用还是为了自己不记得时好查看一下好了0.0
  一般来说应该是UINavigationController包含UINavigationBar和UIToolbar;然后UINavigationBar中又包含了若干个UINavigationItem;UIToolbar也包含如干个UIBarButtonItem的。这个确实如此,不过在UIViewController的扩张类中有navigationItem和toolbarItems的属性可以管理UINavigationController里面定义的UINavigationItem和UIToolbar。
  注意:这里UIBarButtonItem是专门针对一个的UIToolbar或者UINavigationBar对象放置在一个按钮。
  navigationBar默认 是显示的,而toolbarHidden则是默认隐藏的,如需更改显示状态的话可以调用以下代码来实现,这两个都是UINavigationController下定义的属性,同时也有对应的设置方法的。
代码中设置:
  self.navigationController.navigationBarHidden = YES;
    self.navigationController.toolbarHidden = NO;
  
 
 


 下面根据不同的对象来介绍UINavigationController相关的一些类:

  //UINavigationBar
  好吧,到现在为止在使用UINavigationController的时候我是很少很少使用到UINavigationBar来进行相关设置的,很多的时候都是直接跳过self.navigationItem来管理的。不过在使用系统NavgationController的时候有些属性还是很有用的,记录下:
//titleTextAttributes(ios5.0以后可用)这是UINavigationBar的一个属性,通过它你可以设置title部分的字体、字号、阴影等
@property(nonatomic,copy) NSDictionary *titleTextAttributes NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

//UINavigationBar的这个属性可以设置navigation的内容和其中的按钮的颜色
@property(nonatomic,retain) UIColor *tintColor;

代码调用:
  NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor redColor] forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    self.navigationItem.hidesBackButton = YES;

self.navigationController.navigationBar.tintColor = [UIColor greenColor];
 
  //UINavigationItem
  通常情况下我们在某个视图控制器中会通过self.navigationItem来管理系统的UINavigationController中的导航栏中的内容,UINavigationItem下的很多属性都是很常会用到的:
@property(nonatomic,copy)   NSString        *title;     //导航栏的标题
@property(nonatomic,retain) UIView          *titleView; //导航栏的自定义视图,设定之后title就没效果了
@property(nonatomic,copy)   NSString *prompt;     //设置了这个属性值之后,导航栏会加高30,在导航栏标题上方显示该值
@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;//自定义导航栏的左按钮,默认是返回按钮
@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;//自定义导航栏的左按钮,默认是没有东西显示


//代码示例如下
self.navigationItem.title = @"Root"; //设置标题,其实效果和self.title = @"Root";差不多
self.navigationItem.prompt  =@"prompt test";

//在导航栏中添加segment
NSArray *array = [NSArray arrayWithObjects:@"12",@"34", nil];
UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
[segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedController;

//左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;

//右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;


//下面这两个属性设置可同时设置多个左、右按钮,设置方法和单个的差不多,只是先给归类到一个数组中;不过最好不要和设置单个的混合使用
@property(nonatomic,copy) NSArray *leftBarButtonItems NS_AVAILABLE_IOS(5_0);//设置多个左按钮,从左往右排列
@property(nonatomic,copy) NSArray *rightBarButtonItems NS_AVAILABLE_IOS(5_0);//设置多个左按钮,从右往左排列

代码示例:
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:leftButton,leftButton1, nil];
 
  //UIToolbar
  一般我们也都是没用这个系统的工具栏的,不过这里也列一下好了0.0
  UIToolbar一些属性我们也基本没使用,差不多就看作是存放多个UIBarButtonItem的容器好了,同时也大多使用UIViewController的扩张属性toolbarItems来管理好了,直接跳过UIToolbar
  使用的时候先要设置显示工具栏,默认是隐藏的额,然后就可以加入多个UIBarButtonItem了,一般当UIButton使用好了
//代码示例:

self.navigationController.toolbarHidden = NO;

UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
    UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
    UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
     //这个可使UIBarButtonItem自适应排列
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];
 
 
 
posted @ 2013-11-01 16:41  小、  阅读(2294)  评论(0编辑  收藏  举报