iOS开发UINavigation系列一——导航栏UINavigtionBar

iOS开发UINavigation系列一——导航栏UINavigtionBar

1、导航栏的使用

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

2、UINavigationBar的建立和风格类型

        导航栏继承于UIView,因此咱们能够像建立普通视图那样建立导航栏,好比咱们建立一个高度为80的导航栏,将其放在ViewController的头部,代码以下:字体

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

效果以下:动画

        

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

@property(nonatomic,assign) UIBarStyle barStyle;

UIBarStyle是一个枚举,其中大部分的样式都已弃用,有效果的只有以下两个:spa

typedef NS_ENUM(NSInteger, UIBarStyle) {
    UIBarStyleDefault          = 0,//默认
    UIBarStyleBlack            = 1,//黑色
}

默认的风格就是咱们上面看到的白色的风格,黑色的风格效果瑞以下:代理

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

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

@property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

下面一些方法用于设置NavigationBar及上面item的颜色相关属性:对象

@property(null_resettable, nonatomic,strong) UIColor *tintColor;

tintColor这个属性会影响到导航栏上左侧pop按钮的图案颜色和字体颜色,系统默认是以下颜色:继承

@property(nullable, nonatomic,strong) UIColor *barTintColor;

BarTintColor用于设置导航栏的背景色,这个属性被设置后,半透明的效果将失效:图片

- (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]};

咱们也能够经过下面的属性设置导航栏标题的竖直位置偏移:

- (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;

专一技术,热爱生活,交流技术,也作朋友。

——珲少 QQ群:203317592

相关文章
相关标签/搜索