IOS 導(dǎo)航欄、狀態(tài)欄的設(shè)置

在iOS開(kāi)發(fā)過(guò)程中,導(dǎo)航欄和狀態(tài)欄的使用有著毋庸置疑的重要性。尤其遇到一些不怎么會(huì)設(shè)計(jì)的***時(shí)候。所以我們必須對(duì)一些常用屬性有較深的理解才能應(yīng)對(duì)自如。下面是我從項(xiàng)目中總結(jié)的一些導(dǎo)航欄和狀態(tài)欄的使用方法:

一、導(dǎo)航欄的設(shè)置 UINavigationBar

先上兩張官方文檔里面的圖片,各個(gè)屬性描述的很詳細(xì)。


導(dǎo)航欄Item.png

導(dǎo)航欄屬性一覽表.png

1. 導(dǎo)航欄外觀(UINavigationBar)設(shè)置

  //導(dǎo)航欄的背景色設(shè)置
[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
  //坐標(biāo)起始位置設(shè)置
[[UINavigationBar appearance] setTranslucent:NO]; //(0.64)開(kāi)始
  // 創(chuàng)建字典保存 backItem (字體大小,顏色...)設(shè)置
NSMutableDictionary *atts = [NSMutableDictionary dictionary];
atts[NSFontAttributeName] = [UIFont systemFontOfSize:13.0];
atts[NSForegroundColorAttributeName] = [UIColor redColor];
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:atts forState:UIControlStateNormal];
  //創(chuàng)建字典保存topItem(字體大小,顏色...)設(shè)置
NSMutableDictionary *topItemDic = [NSMutableDictionary dictionary];
topItemDic[NSFontAttributeName] = [UIFont systemFontOfSize:18.0];
topItemDic[NSForegroundColorAttributeName] = [UIColor colorWithRed:255/255.0 green:137/255.0 blue:51/255.0 alpha:1];;
[[UINavigationBar appearance] setTitleTextAttributes:topItemDic];
[navi.navigationBar setTitleTextAttributes:topItemDic];
  //設(shè)置導(dǎo)航欄backItem圖片
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"backIcon"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backIcon"]];

2.UINavigationItem設(shè)置

因?yàn)橄到y(tǒng)默認(rèn)leftBarButtonItem距離左邊距離為20,而很多需求都要求小于20。使用以下方法可以實(shí)現(xiàn):

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[btn setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
btn.tintColor = [UIColor cyanColor];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
//設(shè)置 leftBarButtonItem 向左邊靠近10個(gè)點(diǎn)
if(([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?20:0)){
        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                           target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer, backNavigationItem];
}else{
        self.navigationItem.leftBarButtonItem = backNavigationItem;
}

2.透明導(dǎo)航欄

不透明導(dǎo)航欄.png

其實(shí)很簡(jiǎn)單,將導(dǎo)航欄translucent屬性設(shè)置為YES,然后放一張透明圖片即可。

self.navigationController.navigationBar.translucent = YES;  //坐標(biāo)從(0,0)開(kāi)始 
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"透明圖片.png"] forBarMetrics:UIBarMetricsDefault]; 
self.navigationController.navigationBar.clipsToBounds = YES;//去掉白線

此時(shí),導(dǎo)航欄就變成透明的了。


透明導(dǎo)航欄.png

有時(shí),我們會(huì)為了界面的坐標(biāo)從(0,64)開(kāi)始,所以會(huì)將translucent屬性設(shè)置為NO,所以退出該界面時(shí)會(huì)導(dǎo)致其他界面出現(xiàn)問(wèn)題。還有,將導(dǎo)航欄設(shè)置透明之后,push出其他界面是導(dǎo)航欄也會(huì)有問(wèn)題,此時(shí),我們需要在viewWillDisappear中將navigationBar的背景圖片取消:

self.navigationController.navigationBar.translucent = NO;
self.navigationController.navigationBar.clipsToBounds = NO;
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""]forBarMetrics:UIBarMetricsDefault];

二、狀態(tài)欄的設(shè)置 UIStatusBarStyle

狀態(tài)欄,會(huì)因?yàn)閕nfo.plist中View controller-based status bar appearance 的兩種狀態(tài)(YES or NO)需要兩種不同方式進(jìn)行設(shè)置
以下是官方文檔上介紹的 狀態(tài)欄的樣式信息:


狀態(tài)欄樣式.png

// default 黑色的前景,用來(lái)放在亮色背景上;
// lightContent 白色的前景,用來(lái)放在暗色背景上;

View controller-based status bar appearance 設(shè)置為YES時(shí),狀態(tài)欄的設(shè)置方式:

// 狀態(tài)欄的隱藏/顯示
- (BOOL) prefersStatusBarHidden{
    return YES; //隱藏狀態(tài)欄
}
//狀態(tài)欄的背景色 UIBarStyleDefault->黑色
[[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; //白色

View controller-based status bar appearance 設(shè)置為NO時(shí),狀態(tài)欄的設(shè)置方式需要使用UIApplication :

// 隱藏/顯示 狀態(tài)欄
[UIApplication sharedApplication].statusBarHidden = YES; 
//狀態(tài)欄的背景色 UIStatusBarStyleDefault->黑色
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

后續(xù)持續(xù)補(bǔ)充.....

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容