//
// ViewController.m
// 分段控制器,➕滚动试图
#import "ViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
@interface ViewController ()<UIScrollViewDelegate>{
UIScrollView *scr;
UIScrollView *CenterScr;
UIButton *btn;
}
@property (nonatomic , strong)UIButton *selBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"网易新闻";
//添加所以子控制器
[self setUpVc];
//设置顶部滚动试图
[self setUp];
//按钮
[self setBtn];
//设置中间滚动试图
[self setCenter];
}
//设置中间滚动试图
- (void)setCenter{
CenterScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height-108)];
//滚动范围
CenterScr.contentSize = CGSizeMake(self.view.frame.size.width* self.childViewControllers.count, 0);
CenterScr.backgroundColor = [UIColor redColor];
//代理
CenterScr.delegate = self;
CenterScr.pagingEnabled = YES;
CenterScr.showsHorizontalScrollIndicator = NO;
CenterScr.showsVerticalScrollIndicator = NO;
CenterScr.bounces = NO;
[self.view addSubview:CenterScr];
for (int i= 0 ; i<self.childViewControllers.count; i++) {
//获取对应的子控制器
UIViewController *VCL = self.childViewControllers[i];
//设置子控制器view的位置
VCL.view.frame = CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height-108);
[CenterScr addSubview:VCL.view];
}
}
//设置顶部滚动试图
- (void)setUp{
scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
scr.backgroundColor = [UIColor whiteColor];
scr.showsHorizontalScrollIndicator = NO;
scr.contentSize = CGSizeMake(6*100, 0);
[self.view addSubview:scr];
}
//按钮
- (void)setBtn{
//获取当前控制器的子控制器
NSInteger count = self.childViewControllers.count;
for (int i =0; i<count; i++) {
//初始化位置
btn = [[UIButton alloc]initWithFrame:CGRectMake(i*100, 0, 100, 44)];
//绑定标识
btn.tag = i;
//获取对应的子控制器的文字
UIViewController *Vc = self.childViewControllers[i];
if (i == 0) {
[self click:btn];
}
//添加按钮文字
[btn setTitle:Vc.title forState:UIControlStateNormal];
//默认颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//选中后的颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:btn];
}
}
//按钮方法
- (void)click:(UIButton *)btn{
[self selBtn:btn];
//滚动到对应的界面的偏移量
// CGFloat offSetX = btn.tag * self.view.frame.size.width;
//设置中间滚动试图的偏移量
CenterScr.contentOffset = CGPointMake(btn.tag * self.view.frame.size.width, 0);
//添加对应自控制器的view到对应的位置
UIViewController *Vc = self.childViewControllers[btn.tag];
//设置自控制器view的位置
Vc.view.frame = CGRectMake(btn.tag * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[CenterScr addSubview:Vc.view];
}
//点击按钮切换颜色
- (void)selBtn:(UIButton *)btn{
//点击按钮切换颜色
_selBtn.selected = NO;
btn.selected = YES;
_selBtn = btn;
}
//添加所以子控制器
- (void)setUpVc{
//创建热点控制器
OneViewController *one = [OneViewController new];
one.title = @"头条";
//添加到当前控制器
[self addChildViewController:one];
//创建热点控制器
TwoViewController *two = [TwoViewController new];
two.title = @"热点";
//添加到当前控制器
[self addChildViewController:two];
//创建热点控制器
ThreeViewController *three = [ThreeViewController new];
three.title = @"视频";
//添加到当前控制器
[self addChildViewController:three];
//创建热点控制器
FourViewController *four = [FourViewController new];
four.title = @"新闻";
//添加到当前控制器
[self addChildViewController:four];
//创建热点控制器
FiveViewController *five = [FiveViewController new];
five.title = @"社会";
//添加到当前控制器
[self addChildViewController:five];
//创建热点控制器
SixViewController *Six = [SixViewController new];
Six.title = @"科技";
//添加到当前控制器
[self addChildViewController:Six];
}
@end
分段控制器+滚动视图
猜你喜欢
转载自blog.csdn.net/chuck_phonics/article/details/81907162
今日推荐
周排行