美文网首页
自定义Tabbar

自定义Tabbar

作者: 老公123 | 来源:发表于2018-12-12 14:14 被阅读0次

    #import "FxianViewController.h"

    #import "OneViewController.h"

    #import "TwoViewController.h"

    #import "ThreeViewController.h"

    @interface FxianViewController ()<UIScrollViewDelegate>{

        UIScrollView *scr;

        UIButton*bun;

        UIScrollView*center;

    }

    @property (nonatomic,strong)UIButton *selbun;

    @end

    @implementationFxianViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        [selfADDbtn];

        //添加顶部滚动式图

        [self setTopVc];

        //添加点击按钮

        [self setUpBun];

        //设置中间滚动

        [self setCenterVc];

    }

    -(void)ADDbtn{

        OneViewController *one=[[OneViewController alloc]init];

        one.title=@"语文";

        [self addChildViewController:one];

        TwoViewController *two=[[TwoViewController alloc]init];

        two.title=@"数学";

        [self addChildViewController:two];

        ThreeViewController *three=[[ThreeViewController alloc]init];

        three.title=@"数学";

        [self addChildViewController:three];

    }

    - (void)setTopVc{

        scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 100)];

        scr.backgroundColor=[UIColor whiteColor];

        scr.bounces=NO  ;

        [self.view addSubview:scr];

    }

    - (void)setUpBun{

        NSInteger count=self.childViewControllers.count;

        for(NSIntegeri=0; I<count;i++){

            bun=[[UIButton alloc]initWithFrame:CGRectMake(i*100, 0, 100, 40)];

            bun.tag=i;

            NSLog(@"tag :%ld",bun.tag);

            if(i==0) {

                [selfppp: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];

            [scraddSubview:bun];

            scr.contentSize=CGSizeMake(count *100,0);

        }

    }

    - (void)setCenterVc{

        center=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 130, self.view.frame.size.width,self.view.frame.size.height - 130)];

        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*i, 0, self.view.frame.size.width, self.view.frame.size.height);

            [centeraddSubview:vc.view];

        }

    }

    - (void)ppp:(UIButton*)sender{

        [selfselbun: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);

        [centeraddSubview:vc.view];

    }

    - (void)selbun:(UIButton*)sender{

        _selbun.selected=NO;

        sender.selected=YES;

        _selbun=sender;

    }

    相关文章

      网友评论

          本文标题:自定义Tabbar

          本文链接:https://www.haomeiwen.com/subject/pvnuhqtx.html