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

时间:2023-01-15 23:06:35

效果如下:

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

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
@end

ViewController.m

 #import "ViewController.h"

 @interface ViewController ()
- (void)userNotificationDidPush:(UIApplication *)application;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UILabel *lblMessage = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, , )];
lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
lblMessage.text = @"需要在PL文件新增行View controller-based status bar appearance=NO;触摸画面后,切换状态条颜色";
lblMessage.numberOfLines = ;
lblMessage.textAlignment = NSTextAlignmentCenter;
lblMessage.textColor = [UIColor brownColor];
lblMessage.backgroundColor = [UIColor whiteColor];
[self.view addSubview:lblMessage]; self.navigationItem.prompt = @"看看状态栏的颜色变化";
self.navigationItem.title = @"改变状态栏的颜色";
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIApplication *application = [UIApplication sharedApplication];
//状态栏样式切换
if (application.statusBarStyle == UIStatusBarStyleDefault) {
application.statusBarStyle = UIStatusBarStyleLightContent;
} else {
application.statusBarStyle = UIStatusBarStyleDefault;
} [self userNotificationDidPush:application];
} /**
* 扩展知识:关于iOS不同版本的消息通知知识
*
* @param application 共享的UIApplication单例模式对象实例
*/
- (void)userNotificationDidPush:(UIApplication *)application {
//应用程序图标标记数
//因为registerUserNotificationSettings方法为iOS8的方法,无法在iOS8以下版本使用;所以需要分别处理
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
[application registerUserNotificationSettings:settings];
} else {
[application registerForRemoteNotifications];
}
application.applicationIconBadgeNumber = ; //应用程序图标的消息标记数
//self.tabBarItem.badgeValue = @"3"; //底部选项卡的消息标记数 /* 判断Push推送通知是否打开;同上面一样道理需要分别处理
UIRemoteNotificationType types;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
} else {
types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
}
BOOL isEnabledNotification = types & UIRemoteNotificationTypeAlert;
*/
} @end

AppDelegate.h

 #import <UIKit/UIKit.h>

 @interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navigationController; @end

AppDelegate.m

 #import "AppDelegate.h"
#import "ViewController.h" @interface AppDelegate ()
@end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewController *viewController = [[ViewController alloc] init];
_navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
_window.rootViewController = _navigationController;
[_window addSubview:_navigationController.view];
[_window makeKeyAndVisible];
return YES;
} - (void)applicationWillResignActive:(UIApplication *)application {
} - (void)applicationDidEnterBackground:(UIApplication *)application {
} - (void)applicationWillEnterForeground:(UIApplication *)application {
} - (void)applicationDidBecomeActive:(UIApplication *)application {
} - (void)applicationWillTerminate:(UIApplication *)application {
} @end