美文网首页
添加子视图控制器

添加子视图控制器

作者: 西瓜皮奥特曼 | 来源:发表于2017-04-17 11:18 被阅读44次

//添加子视图控制器

- (void) setupChildViewControllers

{

NSArray * vcNames = @[@"PersonageFinancialViewController",@"EnterpriseFinancialViewController"];

for (NSInteger i = 0; i < vcNames.count; i ++) {

NSString * vcName = vcNames[i];

UIViewController * vc = [[NSClassFromString(vcName) alloc] init];

//当执行这句话,不会执行vc的viewDidLoad

[self addChildViewController:vc];

}

//进入子控制器加载第一个页面

[self scrollViewDidEndDecelerating:self.scrollView];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

CGFloat width = SCREEN_WIDTH;

CGFloat heitht = SCREEN_HEIGHT;

CGFloat offset = scrollView.contentOffset.x;

//获取索引值

NSInteger idx = scrollView.contentOffset.x / width;

//根据索引值返回vc引用

UIViewController * vc = self.childViewControllers[idx];

//判断当前vc是否执行过viewDidLoad

if ([vc isViewLoaded]) return;

//设置子控制器view的大小

vc.view.frame = CGRectMake(offset, 0, width, heitht);

//将子控制器的view加入scrollview上

[scrollView addSubview:vc.view];

}

相关文章

网友评论

      本文标题:添加子视图控制器

      本文链接:https://www.haomeiwen.com/subject/xnqtzttx.html