[IOSB]StoryBoard自定义侧边栏
2015-09-30 15:36
696 查看
[IOSB]StoryBoard自定义侧边栏
Demo:http://download.csdn.net/detail/u012881779/9149977
使用故事板方式创建左右侧边栏,主要分四个部分:首页(DMHomeViewController)、左边栏(DMLeftViewController)、右边栏(DMRightViewController)以及管理首页和侧边栏的控制器(DMSidebarViewController)。
主要结构大致这样:
![](https://img-blog.csdn.net/20150930144641145)
设置首页和左右侧边栏的StoryBoard ID:
![](https://img-blog.csdn.net/20150930150006277)
/*DMSidebarViewController*/
由于侧边栏弹出方式是自定义,所以不能从DMSidebarViewController选择Segue使用固定的方式连接首页和侧边栏,需要在代码里面连接。
当在首页之后需要再次使用Segue push/model连接视图时,需要在控制器代码中先push/model一个控制器,之后才能无碍再次使用Segue功能。
/*DMHomeViewController、DMLeftViewController、DMRightViewController*/
在首页和侧边栏中需要使用代理来调用控制器(DMSidebarViewController)中实现的代理方法。
创建代理:
示意图:
左侧栏:
![](https://img-blog.csdn.net/20150930151645653)
首页:
![](https://img-blog.csdn.net/20150930151657685)
右侧:
![](https://img-blog.csdn.net/20150930151706159)
StoryBoard:
Demo:http://download.csdn.net/detail/u012881779/9149977
使用故事板方式创建左右侧边栏,主要分四个部分:首页(DMHomeViewController)、左边栏(DMLeftViewController)、右边栏(DMRightViewController)以及管理首页和侧边栏的控制器(DMSidebarViewController)。
主要结构大致这样:
设置首页和左右侧边栏的StoryBoard ID:
/*DMSidebarViewController*/
由于侧边栏弹出方式是自定义,所以不能从DMSidebarViewController选择Segue使用固定的方式连接首页和侧边栏,需要在代码里面连接。
当在首页之后需要再次使用Segue push/model连接视图时,需要在控制器代码中先push/model一个控制器,之后才能无碍再次使用Segue功能。
#import "DMSidebarViewController.h" #import "DMLeftViewController.h" #import "DMHomeViewController.h" #import "DMRightViewController.h" #import "DMSidebarProtocol.h" @interface DMSidebarViewController () @property (strong, nonatomic) DMLeftViewController *leftvc; @property (strong, nonatomic) DMHomeViewController *homevc; @property (strong, nonatomic) DMRightViewController*rightvc; @end @implementation DMSidebarViewController @synthesize leftvc,homevc,rightvc; - (void)viewDidLoad { [super viewDidLoad]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; homevc = (DMHomeViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DMHomeViewController"]; leftvc = (DMLeftViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DMLeftViewController"]; rightvc = (DMRightViewController *)[storyboard instantiateViewControllerWithIdentifier:@"DMRightViewController"]; CGRect screenBounds = [[UIScreen mainScreen] bounds]; //初始化左、右侧边栏和首页 leftvc.delegate = self; leftvc.nav = self.navigationController; leftvc.view.frame = CGRectMake(-screenBounds.size.width, 0, screenBounds.size.width, screenBounds.size.height); [self.view addSubview:leftvc.view]; homevc.delegate = self; homevc.nav = self.navigationController; homevc.view.frame = CGRectMake(0, 0,screenBounds.size.width, screenBounds.size.height); [self.view addSubview:homevc.view]; rightvc.delegate = self; rightvc.nav = self.navigationController; rightvc.view.frame = CGRectMake(screenBounds.size.width, 0, screenBounds.size.width, screenBounds.size.height); [self.view addSubview:rightvc.view]; } #pragma mark DMSidebarProtocol //左侧边栏弹出 - (void)leftSidebarPopUp:(id)sender{ CGRect screenBounds = [[UIScreen mainScreen] bounds]; float offset = screenBounds.size.width/2.0; [UIView animateWithDuration:0.3 animations:^{ leftvc.view.frame = CGRectMake(-screenBounds.size.width+offset, 0, screenBounds.size.width, screenBounds.size.height); homevc.view.frame = CGRectMake(0+offset, 0,screenBounds.size.width, screenBounds.size.height); rightvc.view.frame = CGRectMake(screenBounds.size.width+offset, 0, screenBounds.size.width, screenBounds.size.height); } completion:nil]; } //右侧边栏弹出 - (void)rightSidebarPopUp:(id)sender{ CGRect screenBounds = [[UIScreen mainScreen] bounds]; float offset = screenBounds.size.width/2.0; [UIView animateWithDuration:0.3 animations:^{ leftvc.view.frame = CGRectMake(-screenBounds.size.width-offset, 0, screenBounds.size.width, screenBounds.size.height); homevc.view.frame = CGRectMake(0-offset, 0,screenBounds.size.width, screenBounds.size.height); rightvc.view.frame = CGRectMake(screenBounds.size.width-offset, 0, screenBounds.size.width, screenBounds.size.height); } completion:nil]; } //侧栏回归原位 - (void)returnSidebarLocation:(id)sender{ CGRect screenBounds = [[UIScreen mainScreen] bounds]; [UIView animateWithDuration:0.3 animations:^{ leftvc.view.frame = CGRectMake(-screenBounds.size.width, 0, screenBounds.size.width, screenBounds.size.height); homevc.view.frame = CGRectMake(0, 0,screenBounds.size.width, screenBounds.size.height); rightvc.view.frame = CGRectMake(screenBounds.size.width, 0, screenBounds.size.width, screenBounds.size.height); } completion:nil]; } @end
/*DMHomeViewController、DMLeftViewController、DMRightViewController*/
在首页和侧边栏中需要使用代理来调用控制器(DMSidebarViewController)中实现的代理方法。
创建代理:
#import <Foundation/Foundation.h> @protocol DMSidebarProtocol <NSObject> @required //左侧边栏弹出 - (void)leftSidebarPopUp:(id)sender; //右侧边栏弹出 - (void)rightSidebarPopUp:(id)sender; //侧栏回归原位 - (void)returnSidebarLocation:(id)sender; @end首页控制器设置:
#import "BaseSidebarViewController.h" #import "DMSidebarProtocol.h" #import "ListViewController.h" @interface DMHomeViewController : BaseSidebarViewController @property (strong, nonatomic) id <DMSidebarProtocol> delegate; @property (strong, nonatomic) UINavigationController *nav; @end @implementation DMHomeViewController - (void)viewDidLoad { [super viewDidLoad]; } //左侧栏弹出 - (IBAction)leftPopUpAction:(id)sender { if(self.view.frame.origin.x == 0){ [_delegate leftSidebarPopUp:nil]; }else{ [_delegate returnSidebarLocation:nil]; } } //右侧栏弹出 - (IBAction)rightPopUpAction:(id)sender { if(self.view.frame.origin.x == 0){ [_delegate rightSidebarPopUp:nil]; }else{ [_delegate returnSidebarLocation:nil]; } } @end
示意图:
左侧栏:
首页:
右侧:
StoryBoard:
相关文章推荐
- iPhone 6s极致体验:3D-Touch+iOS 9系统
- iOS runtime
- Xcode添加类库framwork--iOS开发
- Xcode中设置类库ARC支持--iOS开发
- 自动检测当前运行环境是(开发环境)debug状态还是(真机)release状态--iOS开发
- 为什么要加号"+"方法?为什么方法前面要写“+”号?很多人由于书本的术语太专业了,暂时看不懂,理解不到--iOS开发
- 今天学了一个简单的新技能Xcode6以后创建工程后没有.pch文件,所以来个技能--iOS开发
- iOS-Xcode使用技巧
- 在iOS开发中使用自定义字体
- iOS颜色选择器
- IOS应用信息抓取
- ios软件开发 json格式字符串转字典,字典转json格式字符串
- iOS8中使用CoreLocation定位
- iOS 自定义tabBar
- iOS工程通用模块介绍 二
- 几个iOS工程通用模块介绍
- iOS 分段选择控件 开关按钮 滑杆
- 30个iOS常用动画
- iOS自学
- iOS AutoLayout自动布局中级开发教程(3)-等宽等高等中心