http://www.cnblogs.com/ihojin/p/ios7-statusbarstyle-in-viewcontroller.html
重写NavBar在其他的文件里设置状态栏一直没有用,只有在NavBar设置才能用
In my iPhone application built with Xcode 5 for iOS 7 I set UIViewControllerBasedStatusBarAppearance=YES in info.plist, and in my ViewController I have this code:
-(UIStatusBarStyle) preferredStatusBarStyle{ returnUIStatusBarStyleLightContent; }
还有在plsit里面设置就能用了
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
加入这段代码就可以了,
-----------------------------自定义button----------------------------
// CustomButton.m
// BuyInsurance
// Created by 闪电 on 15/2/11.
// Copyright (c) 2015年 No9527. All rights reserved.
#define TitleButtonImageW LENGTH_4INCH_ADAPTED(10)
#import "CustomButton.h"
@implementation CustomButton
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
// 文字颜色
[self setTitleColor:ColorI(0xffffff) forState:UIControlStateNormal];
// 高亮时不要让imageView变灰色
// self.adjustsImageWhenHighlighted = NO;
self.imageView.contentMode = UIViewContentModeCenter;
// 设置文字右对齐
self.titleLabel.textAlignment = NSTextAlignmentLeft;
// 背景
self.titleLabel.font = FONT_4INCH_ADAPTED_WIDTH(16);
[self setTitleColor:ColorI(0xffffff) forState:UIControlStateNormal];
// [self setTitleColor:ColorI(0xadadad) forState:UIControlStateHighlighted];
}
return self;
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat imageY = 0;
CGFloat imageW = TitleButtonImageW;
CGFloat imageX = 0;
CGFloat imageH = self.height;
return CGRectMake(imageX, imageY, imageW, imageH);
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleX = TitleButtonImageW + LENGTH_4INCH_ADAPTED(5);
CGFloat titleY = 0;
CGFloat titleW = self.width - TitleButtonImageW;
CGFloat titleH = self.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}
@end
------------------------------------重写NavBar------------------------
//
// BaseNavViewController.m
// BuyInsurance
//
// Created by 闪电 on 15/2/11.
// Copyright (c) 2015年 No9527. All rights reserved.
//
#import "BaseNavViewController.h"
#import "CustomButton.h"
@interface BaseNavViewController ()
@end
@implementation BaseNavViewController
+ (void)initialize{
// 设置导航栏主题
[self setupNavTheme];
}
#pragma mark - 设置导航栏主题
+ (void)setupNavTheme{
// 1.获得appearance对象
UINavigationBar *navBar = [UINavigationBar appearance];
// 设置背景
[navBar setBackgroundImage:[UIImage imageAutomaticName:@"navigationbar_background"] forBarMetrics:UIBarMetricsDefault];
// 设置文字
NSDictionary *textAtts = [self setTitleTextFont:FONT_4INCH_ADAPTED_WIDTH(16) fontColor:[UIColor whiteColor]];
[navBar setTitleTextAttributes:textAtts];
}
// 状态栏
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
// 设置文字
+ (NSDictionary *)setTitleTextFont:(UIFont *)font fontColor:(UIColor *)color{
NSMutableDictionary *textAtts = [NSMutableDictionary dictionary];
// 设置文字颜色
textAtts[NSForegroundColorAttributeName] = color;
// 去掉阴影
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0, 0);
shadow.shadowColor = color;
// 模糊度
shadow.shadowBlurRadius = 0;
textAtts[NSShadowAttributeName] = shadow;
// 设置字体
textAtts[NSFontAttributeName] = font;
return textAtts;
}
// 设置导航栏按钮主题
#pragma mark 设置导航栏按钮主题
+ (void)setupItemTheme{
// 1.获得appearance对象
UIBarButtonItem *item = [UIBarButtonItem appearance];
// 2.设置背景
// 设置文字
NSDictionary *AttrFont = [self setTitleTextFont:FONT_4INCH_ADAPTED_WIDTH(16) fontColor:[UIColor orangeColor]];
[item setTitleTextAttributes:AttrFont forState:UIControlStateNormal];
NSMutableDictionary *hightAttrFont = [NSMutableDictionary dictionary];
hightAttrFont.dictionary = AttrFont;
hightAttrFont[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:0.3];
[item setTitleTextAttributes:hightAttrFont forState:UIControlStateHighlighted];
NSMutableDictionary *disabledlAttrFont = [NSMutableDictionary dictionary];
disabledlAttrFont.dictionary = AttrFont;
disabledlAttrFont[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.2];
[item setTitleTextAttributes:hightAttrFont forState:UIControlStateDisabled];
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (IOS7) { // ios 6 不支持
// 清空手势识别器的代理,就能恢复一起滑动移除控制器
self.interactivePopGestureRecognizer.delegate = nil;
}
// [self statusBar];
}
- (void)statusBar{
UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, 0, 0)];
statusBar.size = [UIApplication sharedApplication].statusBarFrame.size;
[self.navigationBar addSubview:statusBar];
statusBar.backgroundColor = [UIColor yellowColor];
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if (self.viewControllers.count > 0) {
// 左上脚的返回按钮
NSInteger count = self.viewControllers.count - 1;
UIViewController *vc = self.viewControllers[count];
UIImage* backImage = [UIImage imageAutomaticName:@"arrow-left-default"];
CGRect backframe = CGRectMake(0,0,LENGTH_4INCH_ADAPTED(100),LENGTH_4INCH_ADAPTED(30));
CustomButton* backButton= [[CustomButton alloc] initWithFrame:backframe];
[backButton setImage:backImage forState:UIControlStateNormal];
// backButton.backgroundColor = [UIColor yellowColor];
[backButton setTitle:vc.title forState:UIControlStateNormal];
// backButton.imageEdgeInsets = UIEdgeInsetsMake(0, LENGTH_4INCH_ADAPTED(-30), 0, 0);
// backButton.titleEdgeInsets = UIEdgeInsetsMake(0, LENGTH_4INCH_ADAPTED(-15), 0, 0);
backButton.titleLabel.font = FONT_4INCH_ADAPTED_WIDTH(16);
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
viewController.navigationItem.leftBarButtonItem = leftBarButtonItem;
}
[super pushViewController:viewController animated:animated];
}
/**
* 返回
*/
- (void)back
{
[self popViewControllerAnimated:YES];
}
@end
statusBar--NavBar---CustomButton
原文:http://www.cnblogs.com/dream999/p/4287102.html