自定义控制器切换

来源:互联网 发布:100以内手指算法视频 编辑:程序博客网 时间:2024/05/18 01:33

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
最后效果:





0 0
原创粉丝点击