您的位置:首页 > 移动开发 > IOS开发

[IOSB]StoryBoard自定义侧边栏

2015-09-30 15:36 696 查看
[IOSB]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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: