UINavigationBar-使用总结

来源:互联网 发布:lol2016年度数据回顾 编辑:程序博客网 时间:2024/05/16 00:52

转载自:http://blog.sina.com.cn/s/blog_7b9d64af01019zsi.html


多视图应用程序中,我们常常使用到自定义UINavigationBar来完成导航条的设置。

1.获取导航条

UINavigationBar*navBar =self.navigationController.navigationBar;


2.设置导航条样式(使用系统自带样式)

[navBar setBarStyle:UIBarStyleDefault];


分别有如下几种样式:

typedefNS_ENUM(NSInteger, UIBarStyle) {

   UIBarStyleDefault         =0,

   UIBarStyleBlack          =1,

   UIBarStyleBlackOpaque     =1,// Deprecated. UseUIBarStyleBlack

   UIBarStyleBlackTranslucent =2, //Deprecated. Use UIBarStyleBlack and set the translucent property toYES

};


从字面我们就能了解这4种样式的大概意思:
分别为:
UIBarStyleDefault:默认样式
UINavigationBar-使用总结

UIBarStyleBlack:黑色
UINavigationBar-使用总结

UIBarStyleBlackOpaque:黑色不透明
UINavigationBar-使用总结

UIBarStyleBlackTranslucent:黑色透明
UINavigationBar-使用总结

注意:我们发现,在后面两个标记为Deprecated,我们知道使用后面两种将不被提倡。
从枚举中,我们也可以看出:UIBarStyleBlack=1UIBarStyleBlackOpaque=1表示为一样的。
后来,发现增加了一个方法:[navBarsetTranslucent:YES];用来指示是否透明。

所以,我们使用UIBarStyleDefaultUIBarStyleBlack来定义UINavigationBar样式,并且用setTranslucent:方法来设置透明与否。

3.自定义导航条颜色

如果,仅仅使用这4种(2种样式*是否透明),难免太逊了,必须能自定义UINavigationBar样式啊。

if ([navBarrespondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]){

       //UIBarMetricsLandscapePhone

       [navBarsetBackgroundImage:[UIImageimageNamed:@"图片名称"] forBarMetrics:UIBarMetricsDefault];

    }


setBackgroundImage方法的第二个参数,需要解释一下:

UIBarMetricsDefault:用竖着(拿手机)时UINavigationBar的标准的尺寸来显示UINavigationBar

UIBarMetricsLandscapePhone:用横着UINavigationBar标准尺寸来显示UINavigationBar




简介

UINavigationBar是用于实现管理层级关系内容的组件,直接继承自UIView。通常用在UINavgationController类中,用于管理和显示UINavgationController的subViewController , 同时UINavgationBar也可以单独使用,添加至任何的UIView中。UINavigationBar比较重要的属性为,左侧按钮,中间的标题,以及右侧按钮。

设置外观

通过barStyle,titColor,以及translucent属性,我们可以简单的定制UINavgationBar的外观。

其中barStyle对用的样式外观的枚举量包括:

?
1
2
3
4
UIBarStyleDefault,对应一个蓝色渐变背景
UIBarStyleBlack,对应一个不透明的褐色背景样式。
UIBarStyleBlackOpaque,等用于UIBarStyleBlack样式,但规定为弃用类型,
UIBarStyleBlackTranslucent,等用于barStyle设置为UIBarStyleBlack,同时指定translucent属性为YES,规定为弃用类型。


translucent属性控制bar的背景是否拥有部分透明效果,当值设置为YES时,无论是什么样式的navgation bar,其背景都是部分透明的。




添加内容

UINavgationBar虽然直接继承于UIView,但其本身并不是同其它UIView一样通过addSubview去添加内容,比较特殊的是,需要通过navgation item类向其补充指定的内容,包括按钮和标题。究其原因是在设计上UINavgationBar是通过维护一个UINavgationItem对象栈来实现管理具有层级关系的视图内容。通过
?
1
2
3
4
5
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated
 
- (UINavigationItem *)popNavigationItemAnimated:(BOOL)animated
 
- (void)setItems:(NSArray *)items animated:(BOOL)animated

三个方法,来向navgation bar中添加或移除内容。

UINavgationBar的items属性保存了所有的item,为数组类型,通过items属性我们可以遍历所有的item元素。

UINavgationBar的topItem指定了当前navgation bar显示的内容,topItem为栈顶元素,假如当前navgation bar维护了n个items,那么topItem的索引为n-1 ;

UINavgationBar的backItem保存了topItem的下一个item,即索引为n-2的item。如果当前只有一个item,那么该属性为nil,而不是与topItem指向相同的item。

UINavgationItem

该类封装了关于UINavgationBar的对象栈中的显示信息,需要注意的是其直接继承自NSObject类型,从名称上注意不要把其当做是UIView的子类。通过

?
1
- (id)initWithTitle:(NSString *)title

方法来新建一个UINavgationItem对象,其中title则为显示在UINavgationBar中间的文本标题。并且该参数会将文本内容保存在UINavgationItem的title属性中。在新的UINavgationItem对象生成之后,通过改变其title属性,也可以更新UInavgationBar的中间的文本标题内容。同时UINavgationItem提供了titleView属性,来让我们更加灵活的定制UINavgationBar中间显示内容,而不仅限于显示普通的文本标题。有时间在对其进行详细描述,此处只是简单提示一下。本篇不对其进行详细介绍,

设置title样式

UINavgationBar提供了titleTextAttributes 属性来简单的设置其title样式,titleTextAttributes是一个NSDictionary类型,包含的固定的属性名称,可以用来设置title的样式,指定的属性keys声明于NSString UIKit Additions Reference扩展中,包括:
?
1
2
3
4
NSString *constUITextAttributeFont,设置title的文字字体;
NSString *constUITextAttributeTextColor,设置title的文字颜色;
NSString *constUITextAttributeTextShadowColor,设置titlewz的阴影颜色;
NSString *constUITextAttributeTextShadowOffset,设置titlewz阴影的平移量 ;

如,设置title样式为:系统默认bold类型20号红色字体,阴影颜色为白色,右下偏移2像素

?
注:设置title样式可以在具体的页面中设置

1
2
3
4
5
6
7
NSDictionary *navTitleArr = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIFont boldSystemFontOfSize:20],UITextAttributeFont,
                                 [UIColor redColor],UITextAttributeTextColor ,
                                 [NSValue valueWithCGSize:CGSizeMake(2.0, 2.0)] , UITextAttributeTextShadowOffset ,
                                 [UIColor whiteColor] ,UITextAttributeTextShadowColor ,
                                 nil];
[navBar setTitleTextAttributes:navTitleArr];

调整title文字竖直方向偏移

UINavgationBar的title文字默认相对于bar的高度为水平竖直居中,同时UINavgationBar提供了调整文本竖直偏移的方法:

?
1
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics

adjustment指定了偏移量,正值为向下偏移,负值为向上偏移,如设置当前title向下偏移10个像素

?
1
[navBar setTitleVerticalPositionAdjustment:10 forBarMetrics:UIBarMetricsDefault];

设置背景图片

UINavgationBar对外提供了定制其背景图片的方法,
?
1
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics

通过该方法,我们可以自由的来更改navigation bar的背景样式,而不仅仅局限于其默认指定的那几种样式。如,利用编程方式设置背景图片 
?
1
2
3
4
5
6
7
8
UIGraphicsBeginImageContext(navBar.frame.size) ;
CGContextRef ctx = UIGraphicsGetCurrentContext() ;
CGContextSetFillColorWithColor(ctx, [UIColor scrollViewTexturedBackgroundColor].CGColor) ;
CGContextFillRect(ctx, navBar.bounds) ;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext() ;
UIGraphicsEndPDFContext() ;
     
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];




0 0
原创粉丝点击