addChildViewController

UIViewController中通作方法addChildViewController向其中加载不同的界面;可以用于界面切换
效果:


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

@interface ViewController ()

@end

@implementation ViewController{
    OneViewController *oneVC;
    TwoViewController *twoVC;
    ThreeViewController *threeVC;
    UIViewController *currentVC;
    CGRect frame;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initTabTitle];

    //设置子VC位置
    frame = CGRectMake(0, 80, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    //初始化布局
    oneVC = [[OneViewController alloc] initWithNibName];
    oneVC.view.frame = frame;

    twoVC = [[TwoViewController alloc] initWithNibName];
    twoVC.view.frame = frame;

    threeVC = [[ThreeViewController alloc] initWithNibName];
    threeVC.view.frame = frame;
    //开始加载的子VC
    currentVC = oneVC;
    [self.view addSubview:currentVC.view];
    [self addChildViewController:currentVC];
    //tab的点击事件
    [self.btnOne addTarget:self action:@selector(clickOne) forControlEvents:UIControlEventTouchUpInside];
    [self.btnTwo addTarget:self action:@selector(clickTwo) forControlEvents:UIControlEventTouchUpInside];
    [self.btnThree addTarget:self action:@selector(clickThree) forControlEvents:UIControlEventTouchUpInside];

}


-(void)clickOne{
    [self moveVC:currentVC addVC:oneVC];
}

-(void)clickTwo{
    [self moveVC:currentVC addVC:twoVC];
}

-(void)clickThree{
    [self moveVC:currentVC addVC:threeVC];
}

//切换子VC
-(void)moveVC:(UIViewController *)oldVC addVC:(UIViewController *)newVC{

    [self addChildViewController:newVC];

    [self transitionFromViewController:currentVC toViewController:newVC duration:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{

    } completion:^(BOOL finished) {
        if (finished) {
            //移除当前子VC
            [currentVC removeFromParentViewController];
            //添加新子VC
            currentVC = newVC;
            [self.view addSubview:currentVC.view];
            [self addChildViewController:currentVC];
        }
    }];
}

-(void)initTabTitle{
    int rowWidth = [UIScreen mainScreen].bounds.size.width/3;

    CGRect tabOneFrame = self.btnOne.frame;
    tabOneFrame.origin.x = (rowWidth-tabOneFrame.size.width)/2;
    tabOneFrame.origin.y = 0;
    self.btnOne.frame = tabOneFrame;

    CGRect tabTwoFrame = self.btnTwo.frame;
    tabTwoFrame.origin.x = rowWidth + (rowWidth-tabTwoFrame.size.width)/2;
    tabTwoFrame.origin.y = 0;
    self.btnTwo.frame = tabTwoFrame;

    CGRect tabThreeFrame = self.btnThree.frame;
    tabThreeFrame.origin.x = 2*rowWidth + (rowWidth-tabThreeFrame.size.width)/2;
    tabThreeFrame.origin.y = 0;
    self.btnThree.frame = tabThreeFrame;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

源码:github

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值