文章目录
  1. 1. 工具栏
  • UINavigationBar
    • 一个导航栏一般包括四个对象:UINavigationController、UINavigationBar、UIViewControllerUINavigationItem;其中UINavigationItem存放在UINavigationBar上。
  • 导航栏结构剖析图

  • 定制标题视图
    • 通过NavigationItem的titleView属性,定制标题视图,titleView属性是一个视图类,因此可以添加一个UIView的实例,也可以添加UIView的子类,也可以在UIView的实例中添加子视图。
    1
    2
    3
    4
    UIView *cView = [[UIView alloc]initWithFrame:CGRectMake(0,0,160,44)];
    cView = [UIColor redColor];
    self.navigationItem.titleView = cView;
    [cView release];
  • 定制左、右栏目
    • NavigationItem实例有一个leftBarButtonItem和reightBarButtonItem,而这两个属性又是UIBatButtonItem的实例,可以通过初始化实例,定制。
    • UIBarButtonItem类提供了常用的四个初始化方法,通过这些不同的初始化方法,用户可以得到不同风格的Item。
1
2
3
4
5
6
7
8
// 初始化一个UIBarButtonItem的实例,初始化一个系统的Item
-(id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem taget:(id)target action:(SEL)action;
// 初始化一个带图片的UIButtonItem实例
-(id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
// 初始化一个自带标题的UIBarButtonItem实例
-(id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;
// 初始化一个自定义视图
-(id)initWithCustomView:(UIView *)customView;
  • 设置风格
1
2
3
4
// 设置导航栏的风格为黑色
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 设置导航栏为透明
self.navigationController.navigationBar.translucent = YES;
  • 设置颜色
1
2
3
4
5
6
// 设置导航栏的颜色
self.navigationController.navigationBar。tintColor = [UIColor redColor];
// 设置自定义颜色,注意每一个颜色的范围是0~1之间
[UIColor colorWithRed:0/255.0 green:225/255.0 blue:122/255.0 alpha:1];
// 以图片作为颜色,注意这里是无法设置NavigationBar
[UIColor colorWithPatternImage:[UIImage imageNamed:@"img.png"]];
  • 隐藏返回按钮
1
2
3
// 隐藏返回按钮,后者带动画效果
[self.navigationItem setHidesBackButton:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
  • 设置prompt属性
    • 导航栏的prompt属性,通过navigationItem来设置,其主要作用是用于提示用户。比如,用户正在请求网络数据时,提示用户数据正在被加载,待加载完成后可以将它的值设置为nil,取消显示。

工具栏

  • 创建UIToolBar的实例
1
2
3
4
5
6
7
UIToolBar *toolBar = [[UIToolBar alloc]initWithFrame:CFrectMake(0,460-88,320,44)];
// 初始化UIToolBar的UIBarButtonItem实例,与UINavigationItem中左、右栏目是相同的
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"天气" style:UIBarButtonItemStyleBordered target:self action:nil];
// 向UIToolBar添加UIBarButtonItem
NSArray *itemArray = [NSArray arrayWithObjects:item1,nil];
// 设置UIToolBar的间隔,前者是item之前选择一个合适的列宽,后者用户可以自定义item之间的列宽,需要通过UIBar实例中的width属性来设置。
UIBarButtonSystemItemFlexibleSpace|UIBarButtonSystemItemFixedSpace
  • 导航控制器中的UIToolBar
    • 在导航控制器中会带有一个UIToolBar的实例,但默认是隐藏的,如果需要显示,需要通过[self.navigationController setToolbarHidden:No animated:YES]将其打开。
    • 导航控制器只拥有一个UIToolBar实例,但UIToolBar所拥有的UIBarButtonItem实例,由视图控制器管理。
1
2
3
4
// 将UIBarButtonItem放入数组中,最后添加至UIToolBar中,self表示视图控制器
[self setToolbarItems:itemArray animated:YES];
// 以下代码UIBarButtonItem不会出现在UIToolBar中,且toolbar是制度属性
[self.navigationController.toolbar setItem:itemsArray animated:YES];
1
2
3
4
5
6
7
8
9
10
11
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,460-44-44,320,44)];
toolBar.barStyle = UIBarStyleDefault;
[self.view addSubView:toolBar];
[toolBar release];
UIBarButtonItem *addItem = [[UIBarButton alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *saveItem = [[UIBarButton alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:nil];
UIBarButtonItem *titleItem = [[UIBarButton alloc]initWithTitle:@"title" style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *imageItem = [[UIBarButton alloc]initWithImage:[UIImage imageNamed:@"1"] style:UIBarButtonItemStylePlain target:self action:nil];
UIBarButtonItem *flexibleItem = [[UIBar]
NSArray *items = @[addItem,saveItem,titleItem,imageItem];
[toolBar setItems:items animated:Yes];
文章目录
  1. 1. 工具栏