關(guān)于自定義導(dǎo)航欄返回按鈕

1. 普通的自定義導(dǎo)航按鈕

1> 只有返回圖標(biāo)的

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,12,18);

[leftBtnsetBackgroundImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

2> 有圖標(biāo) 有文字

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,90,25);

[leftBtnsetImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnsetTitle:@"添加好友"forState:UIControlStateNormal];

[leftBtnsetTitleColor:[UIColorcolorWithRed:22/255.0green:88/255.0blue:224/255.0alpha:1]forState:UIControlStateNormal];

leftBtn.titleLabel.font= [UIFontsystemFontOfSize:15];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

3> 有的時(shí)候圖標(biāo)和文字之間的距離會(huì)顯的太近 ,或者這個(gè)自定義的返回bar會(huì)距離左邊太近

對(duì)于圖標(biāo)和文字距離太近,其實(shí)很簡(jiǎn)單 : [leftBtnsetTitle:@" ? 添加好友"forState:UIControlStateNormal];

在文字之前加幾個(gè)空格 ,看你滿意嘍?

對(duì)于整個(gè)返回按鈕距離左邊太近 ,我們可以這么寫

//自定義導(dǎo)航左側(cè)按鈕

UIButton* leftBtn = [UIButtonbuttonWithType:UIButtonTypeSystem];

leftBtn.frame=CGRectMake(0,0,90,25);

[leftBtnsetImage:[UIImageimageNamed:@"icon_back"]forState:UIControlStateNormal];

[leftBtnsetTitle:@"添加好友"forState:UIControlStateNormal];

[leftBtnsetTitleColor:[UIColorcolorWithRed:22/255.0green:88/255.0blue:224/255.0alpha:1]forState:UIControlStateNormal];

leftBtn.titleLabel.font= [UIFontsystemFontOfSize:15];

[leftBtnaddTarget:selfaction:@selector(leftBarBtnClicked)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem*leftBarButtonItem = [[UIBarButtonItemalloc]initWithCustomView:leftBtn];

//解決按鈕不靠左的問題.

UIBarButtonItem*nagetiveSpacer = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];

nagetiveSpacer.width= -15;//這個(gè)值可以根據(jù)自己需要自己調(diào)整

self.navigationItem.leftBarButtonItems=@[nagetiveSpacer, leftBarButtonItem];


就這么多 over。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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