iOS开发-微博客户端-基本界面搭建

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

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];

return YES;

}

2>LaunchImage配置 LaunchImage.launchimage文件下的Contents.json文件中记录了LaunchImage的详细配置:

3>取消APP图标渲染

4>程序加载时隐藏状态栏

在程序加载完成后如需恢复状态栏显示,可以在didFinishLaunchingWithOptions方法中调用[applicationsetStatusBarHidden: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= [UIImageimageNamed:@"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= [UIImageimageNamed:imageName];

UIImage*image = [UIImageimageNamed:selectedImageName];

if(iOS7) {

image = [imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

}

[viewController.tabBarItemsetSelectedImage:image];

[selfaddChildViewController: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.0green:arc4random_uniform(256)/255.0blue:arc4random_uniform(256)/255.0alpha:1.0]

#define iOS7 [[UIDevice currentDevice].systemVersion doubleValue] >=7.0

#endif

由于imageWithRenderingMode方法只在iOS7环境下有效,因此此处代码需要添加条件判断语句进行系统适配,通过获取当前运行环境的系统版本来判断是否编译此方法;

8>图片适配 为UIImage添加一个分类,用于image的系统适配:

@implementationUIImage (Extension)

+ (UIImage*)imageWithName:(NSString*)imageName

{

UIImage*image =nil;

if(iOS7) {

NSString*name = [imageNamestringByAppendingString:@"_os7"];

image = [UIImageimageNamed:name];

}

if(!image) {

image = [UIImageimageNamed:imageName];

}

returnimage;

}

@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航