首先我們要?jiǎng)?chuàng)建幾個(gè)View用來(lái)滾動(dòng)下面是我示例的
<UIScrollViewDelegate,UIPopoverPresentationControllerDelegate>{
NSMutableArray *arr;
UIButton *btn;
}
下面如上圖所示
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) OneViewController *oneVC;
@property (nonatomic, strong) TwoViewController *twoVC;
@property (nonatomic, strong) ThreeViewController *threeVC;
@property (nonatomic, strong) FourViewController *fourVC;
@property (nonatomic, strong) FiveViewController *fiveVC;
@property (nonatomic, strong) SixViewController *sixVC;
然后是一些具體的實(shí)現(xiàn)代碼部分(viewDidLoad)
//? ? 創(chuàng)建一個(gè)view,放分段控制器
UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 30)];
//? ? vv.backgroundColor = [UIColor redColor];
[self.view addSubview:vv];
//? ? 存儲(chǔ)分段控制器標(biāo)題
arr = [NSMutableArray arrayWithObjects:@"移動(dòng)通訊",@"傳媒",@"軟工",@"網(wǎng)工",@"云計(jì)算",@"建筑",nil];
// 適應(yīng)scrollView
self.automaticallyAdjustsScrollViewInsets = NO;
//? ? 創(chuàng)建分段控制器
self.segmentedControl = [[UISegmentedControl alloc]initWithItems:arr];
self.segmentedControl.frame = CGRectMake(0, 0, 386, 30);
//? ? 加載到vv上
[vv addSubview:self.segmentedControl];
//? ? self.navigationItem.titleView = self.segmentedControl;
[self.segmentedControl addTarget:self action:@selector(segmentedControlAction:) forControlEvents:UIControlEventValueChanged];
//? ? 默認(rèn)第一個(gè)視圖
self.segmentedControl.selectedSegmentIndex = 0;
// 創(chuàng)建scrollView
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 94, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64)];
[self.view addSubview:self.scrollView];
// 設(shè)置scrollView的內(nèi)容
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, [UIScreen mainScreen].bounds.size.height - 64);
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces = YES;
// 創(chuàng)建控制器
self.oneVC = [OneViewController new];
self.twoVC = [TwoViewController new];
self.threeVC = [ThreeViewController new];
self.fourVC = [FourViewController new];
self.fiveVC = [FiveViewController new];
self.sixVC = [SixViewController new];
// 添加為self的子控制器
[self addChildViewController:self.oneVC];
[self addChildViewController:self.twoVC];
[self addChildViewController:self.threeVC];
[self addChildViewController:self.fourVC];
[self addChildViewController:self.fiveVC];
[self addChildViewController:self.sixVC];
//? ? 每個(gè)視圖
self.oneVC.view.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.twoVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.threeVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*2, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.fourVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*3, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.fiveVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*4, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
self.sixVC.view.frame = CGRectMake([UIScreen mainScreen].bounds.size.width*5, 0, self.scrollView.frame.size.width, CGRectGetHeight(self.scrollView.frame));
//加載到滾動(dòng)視圖上
[self.scrollView addSubview:self.oneVC.view];
[self.scrollView addSubview:self.twoVC.view];
[self.scrollView addSubview:self.fourVC.view];
[self.scrollView addSubview:self.fiveVC.view];
[self.scrollView addSubview:self.threeVC.view];
[self.scrollView addSubview:self.sixVC.view];
// 設(shè)置scrollView的代理
self.scrollView.delegate = self;
//? ? 按鈕
btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//按鈕位置
btn.frame = CGRectMake(385, 0, 30, 30);
//? ? 按鈕背景顏色
btn.backgroundColor = [UIColor whiteColor];
[btn setTitle:@"+" forState:0];
btn.layer.borderWidth = 1;
btn.layer.borderColor = [UIColor blueColor].CGColor;
//? ? 按鈕點(diǎn)擊事件
[btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
//? ? 加載
[vv addSubview:btn];
代理方法
//分段控制器方法
- (void)segmentedControlAction:(UISegmentedControl *)sender
{
[self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * self.scrollView.frame.size.width, 0) animated:YES];
}
//滾動(dòng)視圖代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger n = scrollView.contentOffset.x / scrollView.frame.size.width;
self.segmentedControl.selectedSegmentIndex = n;
}