IOS项目开发布局三
上次讲到项目文件夹的模版,下面我开始以侧边栏为例,继续讲框架。
首先看文件的结构布局图如下:
1. 在Libs的SlideViewController文件夹下添加侧边栏第三方文件
2. 使用侧边栏代码在DemAppDelegate中添加如下:(主要代码)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
SLVMainViewController *mainViewController = [[SLVMainViewController alloc] init];
mainViewController.view.backgroundColor = [UIColor greenColor];
self.sideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:mainViewController];
self.sideViewController.delegate = self;
[mainViewController release];
self.window.rootViewController = self.sideViewController;
[self.window makeKeyAndVisible];
return YES;
}
3. 在Constants文件中定义一些全局的宏4. DemViewController.h和DemViewController.m 是所有ViewController的基类,在里面可以添加一些公用的代码
代码片段如下:
- (id)init
{
if (self = [super init]) {
if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
[self setAutomaticallyAdjustsScrollViewInsets:NO];
}
if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
[self setEdgesForExtendedLayout:UIRectEdgeNone];
}
}
return self;
}
说明如下:
在init方法中添加的代码是为了适配IOS7和IOS7以前的版本,因为IOS7的视图会占用状态栏。
5. 在DemMainViewController中添加需要注意的代码如下
- (void)addMainController
{
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor blueColor];
viewController.title = @"主页面";
viewController.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(showLeftController)];
UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
contentLabel.center = CGPointMake(viewController.view.bounds.size.width/2, (viewController.view.bounds.size.height - 64)/2);
contentLabel.text = @"主视图";
contentLabel.textColor = [UIColor redColor];
contentLabel.backgroundColor = [UIColor clearColor];
contentLabel.font = [UIFont systemFontOfSize:30.f];
[viewController.view addSubview:contentLabel];
[contentLabel release];
_mainNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
[_mainNavController.view setFrame:self.view.bounds];
[viewController release];
[self.view addSubview:_mainNavController.view];
}
说明:注意红色部分的代码,如果不添加这一行代码,导航栏视图会下移20个像素
6. 运行的效果图如下:
IOS6系统的运行图
IOS7系统的运行图
提供项目工程的下载地址如下:http://download.csdn.net/detail/chchong1234/6979661
原文:http://blog.csdn.net/chchong1234/article/details/20224467