您的位置:首页 > 其它

自定义控制器切换

2016-02-09 17:54 267 查看
1.先添加一个view(菜单栏)并添加约束



2.再往 菜单栏view中,添加3个按钮控件,等高等宽



3.让菜单栏中3个按钮都连线到控制器的buttonclick方法上,并创建3个控制器



4.父控制代码

//
//  ViewController.m

#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"

@interface ViewController ()
/** 正在显示的控制器 */
@property (nonatomic, weak) UIViewController *showingVc;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 通过addChildViewController添加的控制器都会存在于childViewControllers数组中
[self addChildViewController:[[OneViewController alloc] init]];
[self addChildViewController:[[TwoViewController alloc] init]];
[self addChildViewController:[[ThreeViewController alloc] init]];
}

- (IBAction)buttonClick:(UIButton *)sender
{
// 移除其他控制器的view
[self.showingVc.view removeFromSuperview];

// 获得控制器的位置(索引)
NSUInteger index = [sender.superview.subviews indexOfObject:sender];

// 添加控制器的view
self.showingVc = self.childViewControllers[index];
self.showingVc.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
[self.view addSubview:self.showingVc.view];
}

@end
最后效果:

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