- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//1>創建窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//2>設置窗口的根控制器
UITabBarController *tabBarController = [[UITabBarController alloc] init];
self.window.rootViewController = tabBarController;
//3>顯示窗口
[self.window makeKeyAndVisible];
return YES;
}
LaunchImage.launchimage文件下的Contents.json文件中記錄了LaunchImage的詳細配置:
在程序加載完成后如需恢復狀態欄顯示,可以在didFinishLaunchingWithOptions方法中調用[application setStatusBarHidden:NO]方法;
自定義一個TabBarViewController類繼承UITabBarController類用來創建自定義的TabBarView,并在該類中的viewDidLoad方法中創建子控制器
- (void)viewDidLoad
{
[super viewDidLoad];
//添加子控制器
UIViewController *home = [[UIViewController alloc] init];
home.view.backgroundColor = [UIColor redColor];
home.tabBarItem.title = @"首頁";
home.tabBarItem.image = [UIImage imageNamed:@"tabbar_home"];
[home.tabBarItemsetSelectedImage:[UIImage imageNamed:@"tabbar_home_selected"]];
[self addChildViewController:home];
UIViewController *message = [[UIViewControlleralloc] init];
message.view.backgroundColor = [UIColor orangeColor];
message.tabBarItem.title = @"消息";
message.tabBarItem.image = [UIImage imageNamed:@"tabbar_message_center"];
[message.tabBarItem setSelectedImage:[UIImage imageNamed:@"tabbar_message_center_selected"]];
[self addChildViewController:message];
UIViewController *discover = [[UIViewControlleralloc] init];
discover.view.backgroundColor = [UIColor greenColor];
discover.tabBarItem.title = @"發現";
discover.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
[discover.tabBarItem setSelectedImage:[UIImage imageNamed:@"tabbar_discover_selected"]];
[self addChildViewController:discover];
UIViewController *PRofile = [[UIViewController alloc] init];
profile.view.backgroundColor = [UIColor blueColor];
profile.tabBarItem.title = @"我";
profile.tabBarItem.image = [UIImage imageNamed:@"tabbar_profile"];
[profile.tabBarItem setSelectedImage:[UIImage imageNamed:@"tabbar_profile_selected"]];
[self addChildViewController:profile];
}
在iOS7中,會對selectedImage的圖片再次渲染為藍色,要想顯示原圖,就必須要取消渲染;
取消渲染調用的方法:
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
將添加子控制器到TabBarViewController的代碼進行優化,建立如下方法:
- (void)addOneChildViewController:(UIViewController *)viewController withTitle:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
viewController.view.backgroundColor = ZFRandomColor;
viewController.tabBarItem.title = title;
viewController.tabBarItem.image = [UIImage imageNamed:imageName];
UIImage *image = [UIImage imageNamed:selectedImageName];
if (iOS7) {
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
[viewController.tabBarItem setSelectedImage:image];
[self addChildViewController:viewController];
}
其中ZFRandomColor和iOS7為自定義宏,其宏定義在Prefix.pch文件下:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#define ZFRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
#define iOS7 [[UIDevice currentDevice].systemVersion doubleValue] >= 7.0
#endif
由于imageWithRenderingMode方法只在iOS7環境下有效,因此此處代碼需要添加條件判斷語句進行系統適配,通過獲取當前運行環境的系統版本來判斷是否編譯此方法;
為UIImage添加一個分類,用于image的系統適配:
@implementation UIImage (Extension)
+ (UIImage *)imageWithName:(NSString *)imageName
{
UIImage *image = nil;
if (iOS7) {
NSString *name = [imageName stringByAppendingString:@"_os7"];
image = [UIImage imageNamed:name];
}
if (!image) {
image = [UIImage imageNamed:imageName];
}
return image;
}
@end
新聞熱點
疑難解答