iOS容器视图控制器的使用

 

在iOS的的布局中,很多时候需要自定义状态栏,或者在一个页面上需要把button点击切换不同的视图,这时候该怎么办呢?很明显,这个就需要用到容器视图控制器,这种写法在iOS开发中经常碰到!下面贴上代码,不多!

 1 #import "ViewController.h"
 2 #import "FirstViewController.h"
 3 #import "SecondViewController.h"
 4 
 5 @interface ViewController ()
 6 @property (nonatomic,strong)UIButton         *button;
 7 @property (nonatomic,strong)NSArray          *viewControllers;
 8 @property (nonatomic,strong)UIView           *contentView;
 9 @property (nonatomic,strong)UIViewController *currentController;
10 
11 @end
12 
13 @implementation ViewController
14 
15 - (void)viewDidLoad {
16     [super viewDidLoad];
17     self.view.backgroundColor = [UIColor whiteColor];
18     NSArray *arrayTitle = @[@"button1",@"button2"];
19     //循环创建两个button
20     for (int i = 0; i<2; i++) {
21         _button= [UIButton buttonWithType:UIButtonTypeCustom];
22         _button.frame = CGRectMake(60 + i *100, 100, 100, 40);
23         _button.layer.borderWidth = 1.0;
24         _button.layer.borderColor = [UIColor grayColor].CGColor;
25         _button.backgroundColor = [UIColor clearColor];
26         [_button setTitle:arrayTitle[i] forState:UIControlStateNormal];
27         [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
28         _button.tag = 100 + i;
29         [_button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
30         [self.view addSubview:_button];
31     }
32     FirstViewController *first = [[FirstViewController alloc]init];
33     SecondViewController *second = [[SecondViewController alloc]init];
34     _viewControllers = @[first,second];
35     
36     //初始化容器视图
37     _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 200, CGRectGetMaxX(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 200)];
38     [self.view addSubview:_contentView];
39     [self setCurrentControllerWithIndex:0];//默认选择第0个controller
40 }
41 
42 
43 //切换控制器
44 - (void)setCurrentControllerWithIndex:(NSInteger)index
45 {
46     //表现形式,可视化的把一个controller的试图内容如到容器controller的视图中
47     UIViewController *controller = [_viewControllers objectAtIndex:index];
48     if (_currentController == controller) {
49         return;
50     }
51     //把controller关联到容器controller中
52     [_contentView addSubview:controller.view];
53     [self addChildViewController:controller];
54     //通知系统,关联完毕
55     [controller didMoveToParentViewController:self];//关联controller
56     
57     //移除上一个页面
58     if (_currentController) {
59         //和添加viewController-- 对应
60         [_currentController.view removeFromSuperview];
61         [_currentController removeFromParentViewController];
62         [_currentController willMoveToParentViewController:self];
63     }
64     _currentController = controller;
65 }
66 
67 - (void)buttonPressed:(UIButton *)sender{
68     NSUInteger index = sender.tag - 100;
69     [self setCurrentControllerWithIndex:index];
70 
71 }

 

还有一种是视图加载,很简单,如下,顺便贴上简单的右切换动画代码:

 1 WJServiceFunctionDetailViewController *function = [[WJServiceFunctionDetailViewController alloc]init];
 2     [self.view addSubview:function.view];
 3     [self addChildViewController:function];
 4     [self.view bringSubviewToFront:function.view];
 5     
 6     [function.view setCenter:CGPointMake(function.view.center.x + CGRectGetMaxX(self.view.bounds), function.view.center.y)];
 7     [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
 8         [function.view setCenter:CGPointMake(function.view.center.x - CGRectGetMaxX(self.view.bounds), function.view.center.y)];
 9     } completion:^(BOOL finished) {
10         [self addChildViewController:function];
11     }];

返回向左的动画

 1 //返回
 2 - (void)comeBack{
 3     [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
 4         [self.view setCenter:CGPointMake(self.view.center.x + CGRectGetMaxX(self.view.bounds), self.view.center.y)];
 5     } completion:^(BOOL finished) {
 6         //移除父视图
 7         [self removeFromParentViewController];
 8         [self.view removeFromSuperview];
 9     }];
10 }

 

posted @ 2015-09-15 10:13  梦影随风  阅读(284)  评论(0编辑  收藏  举报