iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

作者:珲少 时间:2023-07-08 16:52:22 

一、导航栏UINavigationBar
1、导航栏的使用

在iOS开发中,我们通常会使用导航控制器,导航控制器中封装了一个UINavigationBar,实际上,我们也可以在不使用导航控制器的前提下,单独使用导航栏,在UINavigationBar中,也有许多我们可以定制的属性,用起来十分方便。

2、UINavigationBar的创建和风格类型

导航栏继承于UIView,所以我们可以像创建普通视图那样创建导航栏,比如我们创建一个高度为80的导航栏,将其放在ViewController的头部,代码如下:

UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
[self.view addSubview:bar];
效果如下:

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

我们也可以设置导航栏的风格属性,从iOS6之后,UINavigationBar默认为半透明的样式,从上面也可以看出,白色的导航栏下面透出些许背景的红色。导航栏的风格属性可以通过下面的属性来设置:

@property(nonatomic,assign) UIBarStyle barStyle;
UIBarStyle是一个枚举,其中大部分的样式都已弃用,有效果的只有如下两个:

typedef NS_ENUM(NSInteger, UIBarStyle) {
    UIBarStyleDefault          = 0,//默认
    UIBarStyleBlack            = 1,//黑色
}
默认的风格就是我们上面看到的白色的风格,黑色的风格效果瑞如下:

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

3、导航栏常用属性和方法

从上面我们可以看到,iOS6后导航栏默认都是半透明的,我们可以通过下面的bool值来设置这个属性,设置为NO,则导航栏不透明,默认为YES:

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
下面一些方法用于设置NavigationBar及上面item的颜色相关属性:

@property(null_resettable, nonatomic,strong) UIColor *tintColor;
tintColor这个属性会影响到导航栏上左侧pop按钮的图案颜色和字体颜色,系统默认是如下颜色:

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

@property(nullable, nonatomic,strong) UIColor *barTintColor;
BarTintColor用于设置导航栏的背景色,这个属性被设置后,半透明的效果将失效:

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
- (nullable UIImage *)backgroundImageForBarMetrics:(UIBarMetrics)barMetrics;
上面两个方法用于设置和获取导航栏的背景图案,这里需要注意,默认背景图案是不做缩放处理的,所以我们使用的图片尺寸要和导航栏尺寸匹配,这里面还有一个UIBarMetrics参数,这个参数设置设备的状态,如下:

typedef NS_ENUM(NSInteger, UIBarMetrics) {
    UIBarMetricsDefault,//正常竖屏状态
    UIBarMetricsCompact,//横屏状态
};
//设置导航栏的阴影图片
@property(nullable, nonatomic,strong) UIImage *shadowImage;
//设置导航栏的标题字体属性
@property(nullable,nonatomic,copy) NSDictionary<NSString *,id> *titleTextAttributes;
标题字体属性会影响到导航栏的中间标题,如下:

bar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor redColor]};

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

我们也可以通过下面的属性设置导航栏标题的竖直位置偏移:

- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
- (CGFloat)titleVerticalPositionAdjustmentForBarMetrics:(UIBarMetrics)barMetrics;
还有一个细节,导航栏左侧pop按钮的图案默认是一个箭头,我们可以使用下面的方法修改:

@property(nullable,nonatomic,strong) UIImage *backIndicatorImage;
@property(nullable,nonatomic,strong) UIImage *backIndicatorTransitionMaskImage;
4、导航栏中item的push与pop操作

UINavigationBar上面不只是简单的显示标题,它也将标题进行了堆栈的管理,每一个标题抽象为的对象在iOS系统中是UINavigationItem对象,我们可以通过push与pop操作管理item组。

//向栈中添加一个item,上一个item会被推向导航栏的左侧,变为pop按钮,会有一个动画效果
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated;
//pop一个item
- (nullable UINavigationItem *)popNavigationItemAnimated:(BOOL)animated;
//当前push到最上层的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *topItem;
//仅次于最上层的item,一般式被推向导航栏左侧的item
@property(nullable, nonatomic,readonly,strong) UINavigationItem *backItem;
//获取堆栈中所有item的数组
@property(nullable,nonatomic,copy) NSArray<UINavigationItem *> *items;
//设置一组item
- (void)setItems:(nullable NSArray<UINavigationItem *> *)items animated:(BOOL)animated;
5、UINavigationBarDelegate

在UINavigationBar中,还有如下一个属性:

@property(nullable,nonatomic,weak) id<UINavigationBarDelegate> delegate;
通过代理,我们可以监控导航栏的一些push与pop操作:

//item将要push的时候调用,返回NO,则不能push
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item;
//item已经push后调用
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item;
//item将要pop时调用,返回NO,不能pop 
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item;
//item已经pop后调用
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;

二、工具栏UIToolBar
导航栏一般会出现在视图的头部,与之相对,工具栏一般会出现在视图的的底部,上面可以填充一些按钮,提供给用户一些操作。创建一个工具栏如下:
self.view.backgroundColor = [UIColor grayColor];
UIToolbar * tool = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-40, 320, 40)];
[self.view addSubview:tool];

iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析

下面是UIToolBar中的一些方法,其中大部分在UINavigationBar中都有涉及,这里只做简单的介绍:

//工具栏的风格,和导航栏类似,有黑白两种
@property(nonatomic) UIBarStyle barStyle;
//设置工具栏上按钮数组
@property(nullable,nonatomic,copy) NSArray<UIBarButtonItem *> *items;
//设置工具栏是否透明
@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
//设置工具栏按钮
- (void)setItems:(nullable NSArray<UIBarButtonItem *> *)items animated:(BOOL)animated;
//设置item风格颜色
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
//设置工具栏背景色
@property(nullable, nonatomic,strong) UIColor *barTintColor;
//设置工具栏背景和阴影图案
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (nullable UIImage *)backgroundImageForToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;
- (void)setShadowImage:(nullable UIImage *)shadowImage forToolbarPosition:(UIBarPosition)topOrBottom;
- (nullable UIImage *)shadowImageForToolbarPosition:(UIBarPosition)topOrBottom;

标签:iOS,导航栏,工具栏
0
投稿

猜你喜欢

  • 深入理解Java设计模式之状态模式

    2022-07-07 12:49:44
  • C# 正则判断一个数字的格式是否有逗号的代码

    2021-09-13 07:42:12
  • 微服务如何通过feign.RequestInterceptor传递参数

    2022-02-26 00:28:20
  • Java微信授权登陆的实现示例

    2021-12-11 22:03:04
  • java编译器和JVM的区别

    2023-07-18 20:34:48
  • WinForm使用DecExpress控件中的ChartControl插件绘制图表

    2023-09-27 02:21:50
  • Kotlin协程Channel源码示例浅析

    2023-06-14 22:54:08
  • C#如何生成唯一订单号

    2022-11-26 16:32:54
  • Android利用BitMap获得图片像素数据的方法

    2023-06-19 15:50:47
  • Android ContentProvider实现获取手机联系人功能

    2023-08-07 00:52:27
  • IntelliJ Idea常用11款插件(提高开发效率)

    2022-08-31 14:08:23
  • Android使用criteria选择合适的地理位置服务实现方法

    2022-09-26 14:24:34
  • WebFlux 服务编排使用优势详解

    2021-11-27 07:48:45
  • SpringCloud Config分布式配置中心使用教程介绍

    2023-09-15 00:56:06
  • Android Mms之:联系人管理的应用分析

    2022-12-23 20:11:24
  • c#批量抓取免费代理并且验证有效性的实战教程

    2023-12-19 23:33:30
  • C#如何使用Task类解决线程的等待问题

    2023-01-04 18:23:22
  • SpringBoot解决BigDecimal传到前端后精度丢失问题

    2021-12-13 14:44:39
  • Java编程使用箱式布局管理器示例【基于swing组件】

    2022-08-12 14:09:14
  • SpringBoot整合Swagger的方法示例

    2021-05-29 02:42:16
  • asp之家 软件编程 m.aspxhome.com