• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Bo.Ke
博客园    首页    新随笔    联系   管理    订阅  订阅
ios基础篇(十二)——UINavgationController的使用(三)ToolBar

UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏;设置UINavigationController的toolbarHidden属性可显示UIToolBar。

一、UIToolBar的设置

1、在RootViewController.m的viewDidLoad方法中添加代码;

 

[self.navigationController setToolbarHidden:NO animated:YES];

 

如图:显示底部ToolBar

 

2、设置UIToolBar属性:(ios7之后使用)

 

    //设置痕迹颜色
    [self.navigationController.toolbar setBarTintColor:[UIColor greenColor]];

 

如图:

 

//设置背景图片
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"40-15092Z94009"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
    

如图:

3、创建UIBarButtonItem

 UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];
    
    UIBarButtonItem *center = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
    
    //空白
    UIBarButtonItem *blank = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

4、为UIToolBar添加UIBarButtonItem

1)创建一个数组itemsArray;

2)数组上添加UIBarButtonItem;

3)将itemsArray传给UIToolBar;

    NSArray *itemsArray = @[left, blank, center, blank, right];
    [self setToolbarItems:itemsArray];

如图:

 

二、自定义UIToolBar

1、隐藏系统ToolBar:在RootViewController.m 的viewDidLoad方法中添加

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

把新建的Toolbar添加的视图中,并为Toobar设置一个Item,

 
    UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:nil action:nil];
    
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:nil action:nil];
    
    NSArray *buttons = @[one,two];
    
    toolBar = [[UIToolbar alloc] initWithFrame:(CGRect){0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0}];
    
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [toolBar setBarStyle:UIBarStyleDefault];
    [toolBar setItems:buttons];
    [self.view addSubview:toolBar];
    

 UINavigationController的介绍到这里就结束了。

posted on 2015-12-15 13:16  Bo.Ke  阅读(687)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3