1、設定标題:
self.navigationItem.title =@"系統标題";
運作:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiQ3chVEa0V3bT9CX5RXa2Fmcn9CXwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwle5cVYVhWVUZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jNwQTM1EzMxITOxkDM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
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 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"按鈕标題" forState:UIControlStateNormal];
button.backgroundColor = [UIColor yellowColor];
[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 = [[UIBarButtonItem alloc]
initWithTitle:@"左按鈕"
style:UIBarButtonItemStyleDone
target:self
action:@selector(myAction)];
[self.navigationItem setLeftBarButtonItem:leftButton animated:YES];
運作效果如下:
//建立一個UIBarButtonItem用的方法主要有:
[UIBarButtonItem alloc] initWithTitle:(NSString *) style:(UIBarButtonItemStyle) target:(id) action:(SEL)
[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)
4、添加一個右按鈕
在ViewDidLoad方法最後添加代碼:
//添加一個右按鈕
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
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
UIBarButtonItem *rightButton4 = [[UIBarButtonItemalloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
UIBarButtonItem *rightButton5 = [[UIBarButtonItemalloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
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;
運作效果: