ScrollView上添加多个ViewController

今天在搭建项目的时候碰到一个相对内容较多的页面,类似如下图片,产品图片、主要参数、交易条件都有内容。
ScrollView上添加多个ViewController_第1张图片
碰到问题:在进行搭建时碰到Block传值阻塞问题,在其中 一个页面的viewWillApper方法里面实现了block方法,结果不走。


现在先来实现页面的搭建:
我的思路是在一个主viewController中通过ScrollView中间控件,链接四个viewController(基础信息、产品图片、主要参数、交易条件页面)。
具体实现:
(1)定义4个页面:
@property ( strong , nonatomic ) GongYingConditionViewController * gongyingConditionVC;
@property ( strong , nonatomic ) GongYingParameterViewController * gongyingParameterVC;
@property ( strong , nonatomic ) GongYingPhotoViewController * gongyingPhotoVC;
@property ( strong , nonatomic ) BasicInformationViewController * basicInformationVC;
(2)添加四个页面到当前页面的ScrollerView:(因为我的页面是在StoryBoard里面的,所以代码如下)
-( void )setUI
{
   
UIStoryboard * tmpStoryboard = [ UIStoryboard storyboardWithName : @"Main" bundle :[ NSBundle mainBundle ]];
   
   
// 基础信息
   
_basicInformationVC = [tmpStoryboard instantiateViewControllerWithIdentifier : @"BasicInformationViewController" ];
    [
_basicInformationVCviewWillAppear:YES ];
   
//    _gongyingPhotoVC.view.backgroundColor = [UIColor purpleColor];
   
_basicInformationVC . view . frame = CGRectMake ( 0 , 0 , SCREEN_W , _scrollView . frame . size . height );
   
   
// 产品图片
   
_gongyingPhotoVC = [tmpStoryboard instantiateViewControllerWithIdentifier : @"GongYingPhotoViewController" ];
    [
_gongyingPhotoVCviewWillAppear:YES ];
   
//    _gongyingPhotoVC.view.backgroundColor = [UIColor purpleColor];
   
_gongyingPhotoVC . view . frame = CGRectMake ( SCREEN_W , 0 , SCREEN_W , _scrollView . frame . size . height );
   
   
// 主要参数
   
_gongyingParameterVC = [tmpStoryboard instantiateViewControllerWithIdentifier : @"GongYingParameterViewController" ];
    [
_gongyingParameterVCviewWillAppear:YES ];
   
//    _gongyingParameterVC.view.backgroundColor = [UIColor greenColor];
   
_gongyingParameterVC . view . frame = CGRectMake ( SCREEN_W * 2 , 0 , SCREEN_W , _scrollView . frame . size . height );
   
   
// 交易条件
   
_gongyingConditionVC = [tmpStoryboard instantiateViewControllerWithIdentifier : @"GongYingConditionViewController" ];
    [
_gongyingConditionVCviewWillAppear:YES ];
//    _gongyingConditionVC.view.backgroundColor = [UIColor redColor];
   
_gongyingConditionVC . view . frame = CGRectMake ( SCREEN_W * 3 , 0 , SCREEN_W , _scrollView . frame . size . height );
   

    [
_scrollView addSubview : _gongyingPhotoVC . view ];
    [
_scrollView addSubview : _gongyingConditionVC . view ];
    [_scrollViewaddSubview:_gongyingParameterVC.view];
    [ _scrollView addSubview : _basicInformationVC . view ];
    _scrollView . contentSize = CGSizeMake ( SCREEN_W * 4 , SCREEN_H - TOPNAVIGATIONBAR_H - ButtonView_H - DownViewH );
}

注意:代码中画线部分viewWillAppear方法需要手动添加 否则不走。这是我碰到问题的原因所在(具体定义之后为什么不走的原因还不清楚)
(3)以上就可以实现页面添加,现在实现页面切换,切记在添加页面的时候页面的位置很重要,因为是在scrollView中,当你把位置设置好啦,scrollView的 contentSize记得要设置。现在需要实现页面的切换:
只要在点击上面的四个按钮时,及时更改 scrollView的 contentOffset就可以:
_scrollView . contentOffset = CGPointMake ( SCREEN_W *index, 0 );

具体的实现需要自己尝试,如有问题欢迎提问。

你可能感兴趣的:(iOS)