溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

NavigationBar 詳解 設(shè)置

發(fā)布時間:2020-07-07 14:09:31 來源:網(wǎng)絡(luò) 閱讀:4229 作者:li你不知道 欄目:開發(fā)技術(shù)

1、設(shè)置標(biāo)題:


self.navigationItem.title =@"系統(tǒng)標(biāo)題";

運行:

 NavigationBar 詳解 設(shè)置

2、自定義標(biāo)題,設(shè)置titleView:

如果我們想改變標(biāo)題的顏色和字體,就需要自己定義一個UILabel,自己設(shè)置好這個Label的內(nèi)容,可以設(shè)置自己想要的字體、大小和顏色等。然后執(zhí)行self.navigationItem.titleView = myLabel;就可以看到想要的效果。

代碼實現(xiàn):


    //自定義標(biāo)題

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

    titleLable.backgroundColor = [UIColor  clearColor];      //設(shè)置Lable背景的透明

    titleLable.font = [UIFont  boldSystemFontOfSize:20];  //設(shè)置文本字體的大小

    titleLable.textColor = [UIColor  blueColor];   //設(shè)置文本顏色

    titleLable.textAlignment =NSTextAlignmentCenter;  //設(shè)置文本格式位置

    titleLable.text =@"自定義標(biāo)題";    //設(shè)置標(biāo)題

   self.navigationItem.titleView = titleLable;

運行:

 NavigationBar 詳解 設(shè)置

實際上,不僅僅可以將titleView設(shè)置成Label,只要是UIView的對象都可以設(shè)為titleView,例如,將上述代碼改成:


   UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    [button setTitle:@"按鈕標(biāo)題" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor yellowColor];

    [button sizeToFit];

   self.navigationItem.titleView = button;

則運行起來效果如下:

 NavigationBar 詳解 設(shè)置

3、為Navigation Bar添加左按鈕

以下是進(jìn)行l(wèi)eftBarButtonItem設(shè)置的代碼:


    self.navigationItem.leftBarButtonItem = (UIBarButtonItem *)

    self.navigationItem.leftBarButtonItems = (UIBarButtonItem *)

    self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *)

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

    self.navigationItemsetLeftBarButtonItems:(NSArray *)

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

為了在運行時不出錯,我們添加一個空方法,由將要創(chuàng)建的左右按鈕使用:

//空方法

-(void)myAction 

{

}

添加一個左按鈕:

代碼實現(xiàn):


    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]

                                  initWithTitle:@"左按鈕"

                                  style:UIBarButtonItemStyleDone

                                  target:self

                                  action:@selector(myAction)];

    [self.navigationItem setLeftBarButtonItem:leftButton animated:YES];

運行效果如下:

 NavigationBar 詳解 設(shè)置

//創(chuàng)建一個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; 

運行如下:

 NavigationBar 詳解 設(shè)置

這里創(chuàng)建UIBarButtonItem用的方法是


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

 

 

用了系統(tǒng)自帶的按鈕樣式,這些樣式的標(biāo)簽和效果如下

   NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置

 NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置  NavigationBar 詳解 設(shè)置


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

5、添加多個右按鈕

代碼實現(xiàn):


     //添加多個右按鈕

   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;

運行效果如下:

 NavigationBar 詳解 設(shè)置

上面的UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系統(tǒng)提供的用于占位的按鈕樣式。

6、設(shè)置Navigation Bar背景顏色


//設(shè)置navigationBar的背景顏色

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

運行如下:

 NavigationBar 詳解 設(shè)置

7.設(shè)置狀態(tài)條的顏色

由于設(shè)置的是白色,所以基于視圖6.在NavigationController.m中寫入下列代碼:

代碼實現(xiàn):


- (UIStatusBarStyle)preferredStatusBarStyle

{

   return  UIStatusBarStyleLightContent;

}

運行如下:

 NavigationBar 詳解 設(shè)置

8、設(shè)置Navigation Bar背景圖片

代碼實現(xiàn):


    //設(shè)置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];//設(shè)置圖片的大小與Navigation Bar相同

    [self.navigationController.navigationBar

    setBackgroundImage:title_bg

    forBarMetrics:UIBarMetricsDefault]; //設(shè)置背景

添加一個方法用于調(diào)整圖片大?。?/span>


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

   UIGraphicsBeginImageContext(size);

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

   UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();

   UIGraphicsEndImageContext();

    return scaledImage;

}

運行效果:

 NavigationBar 詳解 設(shè)置


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI