設(shè)置NavigationBar的顏色

在開發(fā)APP時(shí),當(dāng)前為了讓APP更加美觀,經(jīng)常會給NavigationBar設(shè)置一個(gè)顏色,如果你直接給navigationBar設(shè)置顏色,

self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];

會發(fā)現(xiàn)根本達(dá)不到效果,呈現(xiàn)出來的顏色根本不是設(shè)置的顏色,這是因?yàn)橄到y(tǒng)的navigation設(shè)置的透明度的原因
那怎樣才能達(dá)到想要的效果呢,你需要一張圖片,然后設(shè)置它的圖片就OK了

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navibar_white"] forBarMetrics:(UIBarMetricsDefault)];

如果你說還需要一張圖片太麻煩了,那你可以給UIImage增加一個(gè)類目color

@interface UIImage (Color)
/**
 *  @brief  根據(jù)顏色生成純色圖片
 *
 *  @param color 顏色
 *
 *  @return 純色圖片
 */
@end

@implementation UIImage (Color)
/**
 *  @brief  根據(jù)顏色生成純色圖片
 *
 *  @param color 顏色
 *
 *  @return 純色圖片
 */
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
@end

代碼就不需要圖片了(這種類目網(wǎng)上一大堆,要學(xué)會利用)

[self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forBarMetrics:(UIBarMetricsDefault)];
// 無色
[self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:(UIBarMetricsDefault)];

以后如果需要純色的圖片,自己生成就好了
比如在我們使用button的時(shí)候,會給button設(shè)置不同狀態(tài)的不同的顏色值,省去了給UI要圖片的麻煩了

[self.codeButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:(UIControlStateNormal)];
    [self.codeButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:(UIControlStateDisabled)];
    [self.codeButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];

正常時(shí)候的顏色,按下去的顏色,不能按時(shí)候的顏色是不是都有了呢
怎么才能讓button不能按呢,只需要一句話

self.codeButton.enabled = NO;

是不是方便很多

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

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

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