iOS UINavigationBar

一、導(dǎo)航欄外觀

外觀 屬性
bar的樣式 barStyle
bar的透明度 translucent
bar的顏色 barTintColor
bar上控件的顏色 tintColor
bar的背景圖片 backgroundImage

二、導(dǎo)航欄內(nèi)容

內(nèi)容 屬性
標(biāo)題 title
標(biāo)題視圖 titleView
左側(cè)按鈕 leftBarButtonItem
右側(cè)按鈕 rightBarButtonItem
返回按鈕 backBarButtonItem
返回按鈕指示圖像 backIndicatorImage
返回按鈕遮罩圖像 backIndicatorTransitionMaskImage

第三、效果顯示

  • 給viewController添加一個導(dǎo)航欄,默認(rèn)效果如下:


    默認(rèn)效果.png
  • 默認(rèn)添加一個子視圖
-(void)test1{
    //默認(rèn)樣式
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    //默認(rèn)透明度為透明[此時添加的控件frame從屏幕最上方開始算起]
    self.navigationController.navigationBar.translucent = YES;
    
    UIView * showView = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 200, 200)];
    showView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:showView];
}
默認(rèn)添加效果.png

、

  • 透明度為不透明
//設(shè)置導(dǎo)航欄的barStyle和translucent
-(void)test2{
    //默認(rèn)樣式
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    //透明度為不透明[此時添加的控件frame從導(dǎo)航欄下面開始算起]
    self.navigationController.navigationBar.translucent = NO;
    
    UIView * showView = [[UIView alloc]initWithFrame:CGRectMake(100, 0, 200, 200)];
    showView.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:showView];
   
}
不透明.png
  • edgesForExtendedLayout
self.edgesForExtendedLayout = UIRectEdgeNone;
透明2.png

第四、導(dǎo)航欄的顏色設(shè)置:barTintColor

self.navigationController.navigationBar.barTintColor = [UIColor purpleColor];
bar顏色.png

第五、導(dǎo)航欄的控件顏色設(shè)置[默認(rèn)為藍(lán)色]tintColor

self.navigationController.navigationBar.tintColor = [UIColor redColor];

第六、設(shè)置背景圖片背景圖片不同的size會展示不同的效果

backgroundImage 
64像素時,會包含狀態(tài)欄
44像素時,只包含導(dǎo)航欄 
小于44時,會疊滿狀態(tài)欄和導(dǎo)航欄
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"圖片名"] forBarMetrics:UIBarMetricsDefault];

第七、UIBarButtonItem

  • 初始化的變化
1、initWithBarButtonSystemItem: target:  action: 
系統(tǒng)提供樣式改變  

UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(pushNextController)];
self.navigationItem.rightBarButtonItem = right;

-(void)pushNextController{
    SecondViewController * secondVc = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVc animated:YES];
}
初始化一.png
2、initWithTitle: style: target: action: 
style設(shè)置為UIBarButtonItemStylePlain 

//使用title初始化,style不能使用邊框類型   
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithTitle:@"Right" style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];
self.navigationItem.rightBarButtonItem = right;
初始化二.png
3、initWithImage: style: target: action: 
圖像需要進(jìn)行渲染,默認(rèn)渲染成模版  

//默認(rèn)---渲染為模版時
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];

//渲染為原圖
UIBarButtonItem * right = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@""] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(pushNextController)];

第八、設(shè)置title和titleView

//設(shè)置title
self.navigationItem.title = @"title";
設(shè)置titleView
self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
titleView.png
//導(dǎo)航欄的控件顏色設(shè)置[默認(rèn)為藍(lán)色]
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

self.navigationItem.titleView = [UIButton buttonWithType:UIButtonTypeContactAdd];
titleView2.png

第九、導(dǎo)航欄設(shè)置backBarButtonItem

  • 沒有title,默認(rèn)顯示back
  • 設(shè)置title,顯示title的內(nèi)容

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"回去" style:UIBarButtonItemStylePlain target:nil action:nil];

back.png

補(bǔ)充

導(dǎo)航欄中設(shè)置控件的image對象都需要進(jìn)行渲染設(shè)置
默認(rèn)是渲染為模版,需要渲染為原圖才能正常顯示。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容