038改变状态栏的颜色(扩展知识:关于iOS不同版本的消息通知知识)

效果如下:

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 @end

ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 - (void)userNotificationDidPush:(UIApplication *)application;
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];   
11 
12     UILabel *lblMessage = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 20, 20)];
13     lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
14     lblMessage.text = @"需要在PL文件新增行View controller-based status bar appearance=NO;触摸画面后,切换状态条颜色";
15     lblMessage.numberOfLines = 0;
16     lblMessage.textAlignment = NSTextAlignmentCenter;
17     lblMessage.textColor = [UIColor brownColor];
18     lblMessage.backgroundColor = [UIColor whiteColor];
19     [self.view addSubview:lblMessage];   
20 
21     self.navigationItem.prompt = @"看看状态栏的颜色变化";
22     self.navigationItem.title = @"改变状态栏的颜色";
23 }
24 
25 - (void)didReceiveMemoryWarning {
26     [super didReceiveMemoryWarning];
27     // Dispose of any resources that can be recreated.
28 }
29 
30 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
31     UIApplication *application = [UIApplication sharedApplication];
32     //状态栏样式切换
33     if (application.statusBarStyle == UIStatusBarStyleDefault) {
34         application.statusBarStyle = UIStatusBarStyleLightContent;
35     } else {
36         application.statusBarStyle = UIStatusBarStyleDefault;
37     }   
38 
39     [self userNotificationDidPush:application];
40 }
41 
42 /**
43  *  扩展知识:关于iOS不同版本的消息通知知识
44  *
45  *  @param application 共享的UIApplication单例模式对象实例
46  */
47 - (void)userNotificationDidPush:(UIApplication *)application {
48     //应用程序图标标记数
49     //因为registerUserNotificationSettings方法为iOS8的方法,无法在iOS8以下版本使用;所以需要分别处理
50     if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
51         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
52         [application registerUserNotificationSettings:settings];
53     } else {
54         [application registerForRemoteNotifications];
55     }
56     application.applicationIconBadgeNumber = 3; //应用程序图标的消息标记数
57     //self.tabBarItem.badgeValue = @"3"; //底部选项卡的消息标记数   
58 
59     /* 判断Push推送通知是否打开;同上面一样道理需要分别处理
60      UIRemoteNotificationType types;
61      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
62      types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
63      } else {
64      types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
65      }
66      BOOL isEnabledNotification = types & UIRemoteNotificationTypeAlert;
67     */
68 }
69 
70 @end

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 
 4 @interface AppDelegate ()
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     [_window addSubview:_navigationController.view];
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21 
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24 
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27 
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30 
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33 
34 @end
原文地址:https://www.cnblogs.com/huangjianwu/p/4576484.html