UINavigationBar和ToolBar的一些属性

本文详细介绍如何在iOS应用中自定义UINavigationBar和UIToolBar的外观与功能,包括改变颜色、设置背景图片、添加按钮及管理隐藏状态等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.基本概念

常用

 //设置信号栏的颜色(白色)

 [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

//修改tabBar上的字的颜色

    mtvc.tabBar.tintColor=[UIColor blueColor];

 

 

//UINavigationBar:继承自UIView,普通的视图控件。每个导航控制器有且只有一个navigationBar,多个视图控制器共用一个navigationBar。

    //iOS7以后,设置导航条的颜色会同时影响信号栏的颜色。

    //拿到navigationBar:self.navigationController.navigationBar

    //1.backgroundColor:修改导航条的背景颜色

    self.navigationController.navigationBar.backgroundColor = [UIColor redColor];

    //2.barTintColor:修改导航条显示的颜色

    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

    //3.设置导航条的背景图片

    //给导航条贴图,会影响view的坐标系统。贴图后,view的原点在导航栏的下方

    //如果要贴的图高度是44 --> 导航栏和状态栏就是分开的 --> iOS7以前的样式

    //如果要贴的图高度是64 --> 导航栏和状态栏仍然是连着的 --> iOS7以后的样式

    //导航栏高度默认是44,横屏状态的高度是32

    //3.1设置竖屏状态导航条的背景图片

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

    //3.2设置横屏状态导航条的背景图片

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav-32.png"forBarMetrics:UIBarMetricsLandscapePhone];

 

 

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    //4.隐藏导航条

    //4.1通过navigationBar的hidden属性隐藏

//    [self.navigationController.navigationBar setHidden:YES];

    //4.2通过导航控制器管理导航条是否隐藏

//    [self.navigationController setNavigationBarHidden:YES];

    //4.3通过导航控制器管理导航条是否隐藏,动画的方式

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

}

 

 

 

 //UINavigationItem

    //UINavigationBar:一个导航控制器有且只有一个navigationBar,导航控制器的栈容器中的 所有的视图控制器共用一个navigationBar。

    //每个视图控制器都可以定制自己的navigationItem

    //每个视图控制器都可通过navigationItem这个属性,来定制导航条上显示的内容

    //1.在导航条左侧放一个返回按钮:设置x,y不起作用

 //导航条上放的元素都必须是UIBarButtonItem类型的。

    //4.1initWithCustomView

    //2.设置标题

//    self.navigationItem.title = @"自定义";

    //3.自定义titleView:设置label作为titleView,label会被居中放在titleView处。

 

 //定宽空格按钮:不能被点击

    UIBarButtonItem *fixSpaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(btnClick:)];

    fixSpaceItem.width = 30;

    //不定宽空格按钮:不能被点击

    UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(btnClick:)];

 

 

    //数组得保证,数组中元素都是UIBarButtonItem类型的对象的地址

    //一组按钮添加在导航条左侧:显示顺序跟添加顺序一致  左 --> 右

    //一组按钮添加在导航条右侧:显示顺序跟添加顺序相反  右 --> 左

    NSArray *arr = @[item0,flexibleSpaceItem,item1];

    self.navigationItem.leftBarButtonItems = arr;

 

 

 

UIToolBar:工具条。

 //导航控制器 下面 实际上还有 工具条 toolBar 只不过默认是隐藏的

    //toolBar 是属于 UINavigationController 的但是 导航的所有子视图控制器 共享 和UINavigationBar 一样 区别就是 导航条在上面 工具条在下面

    //toolBar 的内容 是子视图控制器自己的  内容子视图控制器不共享

    

    //320*44

    //如果要想设置toolBar 必须要显示出来

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

 

 

 

 

 

    //1.让toolBar不隐藏

    //通过当前视图控制器的navigationController属性,拿到管理当前视图控制器的导航控制器的对象。

    //toolBar默认高度是44

    self.navigationController.toolbarHidden = NO;

    //2.给toolBar设置颜色

    //拿到toolBar:self.navigationController.toolbar

    [self.navigationController.toolbar setBarTintColor:[UIColor redColor]];

    //3.设置toolbar的背景图片

    //UIBarMetricsDefault:竖屏状态

    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"toolBar.png"forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];

    //4.toolBarItems:UIBarButtonItem类型的

    //item0:系统的书签按钮bookmarks

    //item1:initWithTitle: --> @"底"

    //item2:customView --> 按钮

    //item3:fixedSpace

    //item4:flexibleSpace

 

  //数组中的每个元素都得是UIBarButtonItem类型的

    NSArray *arr = @[item0,item4,item1,item4,item2];

    self.toolbarItems = arr;

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值