1、设置标题:


self.navigationItem.title =@"系统标题";

运行:

2、自定义标题,设置titleView:

如果我们想改变标题的颜色和字体,就需要自己定义一个UILabel,自己设置好这个Label的内容,可以设置自己想要的字体、大小和颜色等。然后执行self.navigationItem.titleView = myLabel;就可以看到想要的效果。

代码实现:


//自定义标题

UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,44)]; //在这里只有titleLable的高度起作用

titleLable.backgroundColor = [UIColor clearColor]; //设置Lable背景的透明

titleLable.font = [UIFont boldSystemFontOfSize:20]; //设置文本字体的大小

titleLable.textColor = [UIColor blueColor]; //设置文本颜色

titleLable.textAlignment =NSTextAlignmentCenter; //设置文本格式位置

titleLable.text =@"自定义标题"; //设置标题

self.navigationItem.titleView = titleLable;

运行:

实际上,不仅仅可以将titleView设置成Label,只要是UIView的对象都可以设为titleView,例如,将上述代码改成:


UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

[buttonsetTitle:@"按钮标题" forState:UIControlStateNormal];

button.backgroundColor = [UIColoryellowColor];

[button sizeToFit];

self.navigationItem.titleView = button;

则运行起来效果如下:

3、为Navigation Bar添加左按钮

以下是进行leftBarButtonItem设置的代码:


self.navigationItem.leftBarButtonItem = (UIBarButtonItem *)

self.navigationItem.leftBarButtonItems = (UIBarButtonItem *)

self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *)

self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) animated:(BOOL)

self.navigationItemsetLeftBarButtonItems:(NSArray *)

self.navigationItemsetLeftBarButtonItems:(NSArray *) animated:(BOOL)

为了在运行时不出错,我们添加一个空方法,由将要创建的左右按钮使用:

//空方法

-(void)myAction

{

}

添加一个左按钮:

代码实现:


UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]

initWithTitle:@"左按钮"

style:UIBarButtonItemStyleDone

target:self

action:@selector(myAction)];

[self.navigationItemsetLeftBarButtonItem:leftButtonanimated:YES];

运行效果如下:

//创建一个UIBarButtonItem用的方法主要有:


[UIBarButtonItem alloc] initWithTitle:(NSString *) style:(UIBarButtonItemStyle) target:(id) action:(SEL)

[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)

4、添加一个右按钮

在ViewDidLoad方法最后添加代码:

//添加一个右按钮

UIBarButtonItem *rightButton = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemUndo

target:self

action:@selector(myAction)];

self.navigationItem.rightBarButtonItem = rightButton;

运行如下:

这里创建UIBarButtonItem用的方法是


[[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)];

用了系统自带的按钮样式,这些样式的标签和效果如下



注意,UIBarButtonSystemItemPageCurl只能在Tool Bar上显示。

5、添加多个右按钮

代码实现:


//添加多个右按钮

UIBarButtonItem *rightButton1 = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemDone

target:self

action:@selector(myAction)];

UIBarButtonItem *rightButton2 = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

target:nil

action:nil];

UIBarButtonItem *rightButton3 = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemEdit

target:self

action:@selector(myAction)];

UIBarButtonItem *rightButton4 = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace

target:nil

action:nil];

UIBarButtonItem *rightButton5 = [[UIBarButtonItemalloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize

target:self

action:@selector(myAction)];

NSArray *buttonArray = [[NSArray alloc]

initWithObjects:rightButton1,rightButton2,

rightButton3,rightButton4,rightButton5,nil];

self.navigationItem.rightBarButtonItems = buttonArray;

运行效果如下:

上面的UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。

6、设置Navigation Bar背景颜色


//设置navigationBar的背景颜色

self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:79 /255.0green:195 /255.0blue:137 /255.0alpha:1.0];

运行如下:

7.设置状态条的颜色

由于设置的是白色,所以基于视图6.在NavigationController.m中写入下列代码:

代码实现:


- (UIStatusBarStyle)preferredStatusBarStyle

{

return UIStatusBarStyleLightContent;

}

运行如下:


8、设置Navigation Bar背景图片

代码实现:


//设置Navigation Bar背景图片

UIImage *title_bg = [UIImage p_w_picpathNamed:@"title_bg.jpg"];//获取图片

CGSize titleSize =self.navigationController.navigationBar.bounds.size;//获取Navigation Bar的位置和大小

title_bg = [selfscaleToSize:title_bgsize:titleSize];//设置图片的大小与Navigation Bar相同

[self.navigationController.navigationBar

setBackgroundImage:title_bg

forBarMetrics:UIBarMetricsDefault];//设置背景

添加一个方法用于调整图片大小:


- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{

UIGraphicsBeginImageContext(size);

[img drawInRect:CGRectMake(0, 0, size.width, size.height)];

UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return scaledImage;

}

运行效果: