一个UIScrollView左右两边分别放一个viewController的view

一个UIScrollView左右两边分别放一个viewController的view

.h

@property (nonatomic, strong) UIScrollView *bigScrollView; //背景ScrollView

.m

-(void)initView

{

self.view.backgroundColor = BackgroundColorViewController;

NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"左侧",@"右侧",nil];

//初始化UISegmentedControl

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];

segmentedControl.frame = CGRectMake(0, 64 + 5, MAIN_WIDTH, 40);

//设置默认选择项索引

segmentedControl.selectedSegmentIndex = 0;

segmentedControl.tintColor = BackgroundColorNavigation;

[segmentedControl addTarget:self action:@selector(changeSegmentedControl:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:segmentedControl];

_bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64 + 45,MAIN_WIDTH, MAIN_HEIGHT - 64 - 45)];

_bigScrollView.contentSize = CGSizeMake(MAIN_WIDTH*2, MAIN_HEIGHT- 64 - 45);

_bigScrollView.pagingEnabled = YES;

_bigScrollView.bounces = NO;

_bigScrollView.delegate = self;

_bigScrollView.showsHorizontalScrollIndicator = NO;

_bigScrollView.scrollEnabled = NO;

_bigScrollView.backgroundColor = [UIColor greenColor];

[self.view addSubview:_bigScrollView];

}

- (void)changeSegmentedControl:(UISegmentedControl *)sender{

switch (sender.selectedSegmentIndex)

{

case 0:

{

//对应的scrollView显示左侧列表

[_bigScrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.view.bounds.size.width, 0) animated:YES];

[self leftVC];

break;

}

case 1:

{

//对应的scrollView显示右侧列表

[_bigScrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.view.bounds.size.width, 0) animated:YES];

[self rightVC];

break;

}

default:

break;

}

}

#pragma mark - 左右两个列表

- (void)leftVC

{

DeviceShareListViewController *leftShareVC = [[DeviceShareListViewController alloc]init];

leftShareVC.view.frame = CGRectMake(0, 0, MAIN_WIDTH, MAIN_HEIGHT - 64 - 49);

[self addChildViewController:leftShareVC];

[self.bigScrollView addSubview:leftShareVC.view];

}

- (void)rightVC

{

DeviceReceiveListViewController *rightShareVC = [[DeviceReceiveListViewController alloc]init];

rightShareVC.view.frame = CGRectMake(MAIN_WIDTH, 0, MAIN_WIDTH, MAIN_HEIGHT - 64 - 49);

[self addChildViewController:rightShareVC];

[self.bigScrollView addSubview:rightShareVC.view];

}

你可能感兴趣的:(一个UIScrollView左右两边分别放一个viewController的view)