1>創建程序載入界面
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//1>創建窗口
self.window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
//2>設置窗口的根控制器
UITabBarController *tabBarController = [[UITabBarControlleralloc] init];
self.window.rootViewController = tabBarController;
//3>顯示窗口
[self.windowmakeKeyAndVisible];
returnYES;
}
2>LaunchImage配置
LaunchImage.launchimage文件下的Contents.json文件中記錄了LaunchImage的詳細配置:
QQ20140703 1
3>取消APP圖標渲染
QQ20140704 1
4>程序加載時隱藏狀態欄
QQ20140704 2
在程序加載完成後如需恢復狀態欄顯示,可以在didFinishLaunchingWithOptions方法中調用[application setStatusBarHidden:NO]方法;
5>添加TabBar控制器及其子控制器
自定義一個TabBarViewController類繼承UITabBarController類用來創建自定義的TabBarView,並在該類中的viewDidLoad方法中創建子控制器
- (void)viewDidLoad
{
[superviewDidLoad];
//添加子控制器
UIViewController *home = [[UIViewControlleralloc] init];
home.view.backgroundColor = [UIColorredColor];
home.tabBarItem.title = @"首頁";
home.tabBarItem.image = [UIImageimageNamed:@"tabbar_home"];
[home.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_home_selected"]];
[selfaddChildViewController:home];
UIViewController *message = [[UIViewControlleralloc] init];
message.view.backgroundColor = [UIColororangeColor];
message.tabBarItem.title = @"消息";
message.tabBarItem.image = [UIImageimageNamed:@"tabbar_message_center"];
[message.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_message_center_selected"]];
[selfaddChildViewController:message];
UIViewController *discover = [[UIViewControlleralloc] init];
discover.view.backgroundColor = [UIColorgreenColor];
discover.tabBarItem.title = @"發現";
discover.tabBarItem.image = [UIImage imageNamed:@"tabbar_discover"];
[discover.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_discover_selected"]];
[selfaddChildViewController:discover];
UIViewController *profile = [[UIViewControlleralloc] init];
profile.view.backgroundColor = [UIColorblueColor];
profile.tabBarItem.title = @"我";
profile.tabBarItem.image = [UIImageimageNamed:@"tabbar_profile"];
[profile.tabBarItemsetSelectedImage:[UIImageimageNamed:@"tabbar_profile_selected"]];
[selfaddChildViewController:profile];
}
6>渲染圖片
在iOS7中,會對selectedImage的圖片再次渲染為藍色,要想顯示原圖,就必須要取消渲染;
取消渲染調用的方法:
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
7>優化添加子控制器代碼
將添加子控制器到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環境下有效,因此此處代碼需要添加條件判斷語句進行系統適配,通過獲取當前運行環境的系統版本來判斷是否編譯此方法;
8>圖片適配
為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