我们今天来写一个页面滚动按钮
首先我们在AppDelegate.m里创建个导航条
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
我们创建几个viewcontroller作为滚动的视图,我们就先创建六个吧,
然后我们在主视图添加它们
导入头文件
import "oneViewController.h"
import "twoViewController.h"
import "threeViewController.h"
import "fourViewController.h"
import "fiveViewController.h"
import "sixViewController.h"
import "tiaoViewController.h"
遵守协议
UIScrollViewDelegate
设置全局按钮
{
UIScrollView *scr;
UIButton *bun;
UIScrollView *center;
}
@property (nonatomic,strong)UIButton *selbun;
然后
-
(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"问问" style:UIBarButtonItemStylePlain target:self action:@selector(qq1)];
[self setUpVc];
//添加顶部滚动式图
[self setTopVc];
//添加点击按钮
[self setUpBun];
//设置中间滚动
[self setCenterVc];
}
-(void)setUpVc{
oneViewController *one=[[oneViewController alloc]init];
one.title=@"XX";
[self addChildViewController:one];
twoViewController *two=[[twoViewController alloc]init];
two.title=@"XX";
[self addChildViewController:two];
threeViewController *three=[[threeViewController alloc]init];
three.title=@"XX";
[self addChildViewController:three];
fourViewController *four=[[fourViewController alloc]init];
four.title=@"XX";
[self addChildViewController:four];
fiveViewController *five=[[fiveViewController alloc]init];
five.title=@"XX";
[self addChildViewController:five];
sixViewController *six=[[sixViewController alloc]init];
six.title=@"XX";
[self addChildViewController:six];
}
-(void)setTopVc{
scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, self.view.frame.size.width,40)];
scr.bounces=NO ;
self.navigationItem.titleView =scr;
}
-(void)setUpBun{
NSInteger count=self.childViewControllers.count;
for (NSInteger i=0; i
bun.tag=i;
NSLog(@"tag :%ld",bun.tag);
if (i==0) {
[self ppp:bun];
}
UIViewController *vc=self.childViewControllers[i];
[bun setTitle:vc.title forState:UIControlStateNormal];
[bun setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[bun setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[bun addTarget:self action:@selector(ppp:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:bun];
scr.contentSize=CGSizeMake(count *100, 0);
}
}
-(void)setCenterVc{
center=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height)];
//center.backgroundColor=[UIColor greenColor];
[self.view addSubview:center];
center.pagingEnabled=YES;
center.bounces=NO;
center.delegate=self;
center.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*self.childViewControllers.count, 0);
for (int i=0; i
vc.view.frame=CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[center addSubview:vc.view];
}
}
-(void)ppp:(UIButton* )sender{
[self selbun:sender];
//滚动到对应页面的偏移量
CGFloat ofsetX = sender.tag*self.view.frame.size.width;
NSLog(@"%ld",sender.tag);
//设置中间滚动式图的偏移量
center.contentOffset=CGPointMake(ofsetX, 0);
//添加对应子控制器view到对应的位置
UIViewController *vc=self.childViewControllers[sender.tag];
//设置子控制器view的位置
vc.view.frame=CGRectMake(ofsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
[center addSubview:vc.view];
}
-(void)selbun:(UIButton *)sender{
_selbun.selected=NO;
sender.selected=YES;
_selbun=sender;
}
-(void)qq1{
tiaoViewController *tiao=[tiaoViewController new];
[self.navigationController pushViewController:tiao animated:YES];
}