OC/Swift 设置导航栏、状态栏字体与背景的颜色

一、导航栏属性设置

//  swift
//返回键去掉名字
self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "", style: .plain, target: self, action: nil)
// 设置导航栏item指示色
self.navigationController?.navigationBar.tintColor = UIColor.white
/ /设置导航栏背景颜色
self.navigationController?.navigationBar.barTintColor = UIColor(red:0.23, green:0.60, blue:0.93, alpha:1.00)
//  设置导航栏标题颜色
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]


// OC
// 设置导航栏item指示色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
 //  设置导航栏标题颜色与大小
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:17]};
//设置导航栏背景颜色
self.navigationController.navigationBar.barTintColor = CustomBlueColor;

// 在Appdelegate上设置导航控栏背景颜色
[[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]];  
// 设置导航栏上文字的颜色
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
// tabbarbutton默认字体颜色
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor grayColor]} forState:UIControlStateNormal];
// tabbarbutton选中字体颜色
[[UITabBarItem appearance]setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];

二、设置状态栏文字颜色
第一步:在Info.plist中设置UIViewControllerBasedStatusBarAppearanceNO
第二步:在viewDidLoad中加一句

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

这样就可以把默认的黑色改为白色。

设置状态栏背景颜色

// 定义以下方法:
// 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
   }
}
// 调用
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;
    }
}

// 调用
[self setStatusBarBackgroundColor:CustomBlueColor];

你可能感兴趣的:(OC/Swift 设置导航栏、状态栏字体与背景的颜色)