OC/Swift 設(shè)置導(dǎo)航欄、狀態(tài)欄字體與背景的顏色

一、導(dǎo)航欄屬性設(shè)置

//  swift
//返回鍵去掉名字
self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "", style: .plain, target: self, action: nil)
// 設(shè)置導(dǎo)航欄item指示色
self.navigationController?.navigationBar.tintColor = UIColor.white
/ /設(shè)置導(dǎo)航欄背景顏色
self.navigationController?.navigationBar.barTintColor = UIColor(red:0.23, green:0.60, blue:0.93, alpha:1.00)
//  設(shè)置導(dǎo)航欄標題顏色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]


// OC
// 設(shè)置導(dǎo)航欄item指示色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
 //  設(shè)置導(dǎo)航欄標題顏色與大小
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
//設(shè)置導(dǎo)航欄背景顏色
self.navigationController.navigationBar.barTintColor = CustomBlueColor;

// 在Appdelegate上設(shè)置導(dǎo)航控欄背景顏色
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:155/255.0 green:131/255.0 blue:255/255.0 alpha:1]];
// 在Appdelegate上標簽欄的背景顏色
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:155/255.0 green:131/255.0 blue:255/255.0 alpha:1]];  
// 設(shè)置導(dǎo)航欄上文字的顏色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// tabbarbutton默認字體顏色
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
// tabbarbutton選中字體顏色
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];

二、設(shè)置狀態(tài)欄文字顏色
第一步:在Info.plist中設(shè)置UIViewControllerBasedStatusBarAppearanceNO
第二步:在viewDidLoad中加一句

 // swift
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
 // OC
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

這樣就可以把默認的黑色改為白色。

設(shè)置狀態(tài)欄背景顏色

// 定義以下方法:
// swift
func setStatusBarBackgroundColor(color : UIColor) {
    let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
    let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = color
   }
}
// 調(diào)用
self.setStatusBarBackgroundColor(color: UIColor(red:0.23, green:0.60, blue:0.93, alpha:1.00))


//  OC
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
    statusBar.backgroundColor = color;
    }
}

// 調(diào)用
[self setStatusBarBackgroundColor:CustomBlueColor];
最后編輯于
?著作權(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)容