導(dǎo)航欄設(shè)置

設(shè)置導(dǎo)航欄的背景顏色
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
/* 這里不能直接設(shè)置backgroundColor */

設(shè)置后發(fā)現(xiàn)跟設(shè)置的顏色值有偏差,查了下應(yīng)該是上面navigationBar有一層透明視圖的原因沃于,添加如下代碼

The default value is YES. If the navigation bar has a custom background image, the default is YES if any pixel of the image has an alpha value of less than 1.0, and NO otherwise.
If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
If you set this property to NO on a navigation bar with a translucent custom background image, the navigation bar provides an opaque background for the image using black if the navigation bar has UIBarStyleBlack style, white if the navigation bar has UIBarStyleDefault, or the navigation bar’s barTintColor if a custom value is defined.
默認(rèn)為YES涩咖。導(dǎo)航欄有背景圖片的情況下,如過這個(gè)圖片中的任意一個(gè)像素透明度小于1繁莹,則默認(rèn)為YES,否則為NO檩互。
如果當(dāng)導(dǎo)航欄有一個(gè)自定義背景圖片的情況下,你設(shè)置translucent為YES,導(dǎo)航欄會(huì)給這張圖片加上一個(gè)透明度小于1的視圖闸昨。
導(dǎo)航欄在有一個(gè)半透明背景圖片時(shí)候你設(shè)置translucent為NO淹禾,導(dǎo)航欄會(huì)給這個(gè)半透明圖片加上一層不透明的試圖句惯,如果導(dǎo)航欄樣式為UIBarStyleBlack則添加一層黑色試圖,如果導(dǎo)航欄樣式為UIBarStyleDefault或者導(dǎo)航欄已經(jīng)設(shè)置過barTintColor屬性則添加一層白色試圖

self.navigationController.navigationBar.translucent = NO;

一個(gè)坑:原圖上采色計(jì)采集的數(shù)值跟模擬器上顯示出的圖片采集的數(shù)值不一樣不一樣不一樣啊 設(shè)置barTintColor的數(shù)值要在原圖上去采集
原圖:53/217/142
模擬器圖:60/223/153
導(dǎo)航欄顏色:68/227/163 // translucent為YES RGB采集的模擬器上圖片的數(shù)值

顏色對(duì)比
導(dǎo)航欄去橫線

橫線理解為UINavegationBar的shadowImage屬性循诉,文檔中這樣說明

For a custom shadow image to be shown, a custom background image must also be set with the setBackgroundImage:forBarMetrics: method. If the default background image is used, then the default shadow image will be used regardless of the value of this property.

大致意思是說要設(shè)置shadowImage必須先要通過
setBackgroundImage:forBarMetrics:方法給UINavgationBar設(shè)置一個(gè)背景圖片
未設(shè)置前

//UIBarMetricsDefault-豎屏橫屏都有,橫屏導(dǎo)航條變寬茄猫,則自動(dòng)repeat圖片
//UIBarMetricsCompact-豎屏沒有,橫屏有

有橫線

加上下面代碼

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];

效果


去橫線

另一種方法:

@interface ViewController ()
{
    UIImageView *navBarHairlineImageView; // 系統(tǒng)默認(rèn)的那條橫線圖片
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:53.0/255.0 green:217.0/255.0 blue:142.0/255.0 alpha:1];
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
}
/*   可以在這兩個(gè)方法里控制是否顯示橫條
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    navBarHairlineImageView.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    navBarHairlineImageView.hidden = NO;
}
*/

// 找到那條橫線
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末炫惩,一起剝皮案震驚了整個(gè)濱河市阿浓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌芭毙,老刑警劉巖筋蓖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異退敦,居然都是意外死亡粘咖,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門侈百,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瓮下,“玉大人,你說我怎么就攤上這事钝域》砘担” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵例证,是天一觀的道長路呜。 經(jīng)常有香客問我,道長织咧,這世上最難降的妖魔是什么胀葱? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮笙蒙,結(jié)果婚禮上抵屿,老公的妹妹穿的比我還像新娘。我一直安慰自己手趣,他們只是感情好晌该,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布肥荔。 她就那樣靜靜地躺著,像睡著了一般朝群。 火紅的嫁衣襯著肌膚如雪燕耿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天姜胖,我揣著相機(jī)與錄音誉帅,去河邊找鬼。 笑死右莱,一個(gè)胖子當(dāng)著我的面吹牛蚜锨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播慢蜓,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼亚再,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了晨抡?” 一聲冷哼從身側(cè)響起氛悬,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耘柱,沒想到半個(gè)月后如捅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡调煎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年镜遣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片士袄。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖窖剑,靈堂內(nèi)的尸體忽然破棺而出戈稿,到底是詐尸還是另有隱情,我是刑警寧澤鞍盗,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布般甲,位于F島的核電站,受9級(jí)特大地震影響墓造,放射性物質(zhì)發(fā)生泄漏堪伍。R本人自食惡果不足惜帝雇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一尸闸、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧吮廉,春花似錦畸肆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至徽级,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間现使,已是汗流浹背旷痕。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留售碳,地道東北人绞呈。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像艺智,于是被迫代替她去往敵國和親圾亏。 傳聞我的和親對(duì)象是個(gè)殘疾皇子封拧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

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