我們今天來寫一個頁面滾動按鈕
首先我們在AppDelegate.m里創(chuàng)建個導航條
ViewController *vc = [[ViewController alloc]init];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
我們創(chuàng)建幾個viewcontroller作為滾動的視圖,我們就先創(chuàng)建六個吧,
然后我們在主視圖添加它們
導入頭文件
import "oneViewController.h"
import "twoViewController.h"
import "threeViewController.h"
import "fourViewController.h"
import "fiveViewController.h"
import "sixViewController.h"
import "tiaoViewController.h"
遵守協(xié)議
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<count; i++) {
bun=[[UIButton alloc]initWithFrame:CGRectMake(i*80, 6, 100, 26)];
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<self.childViewControllers.count; i++) {
UIViewController *vc=self.childViewControllers[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];
}