首页 > 学院 > 开发设计 > 正文

自定义tabBar

2019-11-07 23:25:35
字体:
来源:转载
供稿:网友
//  //  XNTabBarController.m  //  //  //  Created by neng on 14-6-19.  //  Copyright (c) 2014年 neng. All rights reserved.  //    #import "XNTabBarController.h"  #import "Common.h"  #import "XNTabBarButton.h"    @interface XNTabBarController ()    @PRoperty (nonatomicweak) UIButton *selectedBtn;  @end    @implementation XNTabBarController    (void)viewDidLoad      [super viewDidLoad];      //下面两个方法在开发中是经常会用到的  //    NSLog(@"%s",__func__);  //    NSLog(@"%@",self.view.subviews); //能打印出所有子视图,和其frame      LogFun;      LogSubviews(self.view);          //删除现有的tabBar      CGRect rect self.tabBar.frame;      [self.tabBar removeFromSuperview];  //移除TabBarController自带的下部的条        //测试添加自己的视图      UIView *myView [[UIView alloc] init];      myView.frame rect;      myView.backgroundColor [UIColor redColor];      [self.view addSubview:myView];        for (int 0; 5; i++)          //UIButton *btn [[UIButton alloc] init];          XNTabBarButton *btn [[XNTabBarButton alloc] init];                    NSString *imageName [NSString stringWithFormat:@"TabBar%d", 1];          NSString *imageNameSel [NSString stringWithFormat:@"TabBar%dSel", 1];            [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];          [btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected];            CGFloat i myView.frame.size.width 5;          btn.frame CGRectMake(x, 0, myView.frame.size.width 5, myView.frame.size.height);            [myView addSubview:btn];                    btn.tag i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图            //带参数的监听方法记得加"冒号"          [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];            //设置刚进入时,第一个按钮为选中状态          if (0 == i)              btn.selected YES             self.selectedBtn btn;  //设置该按钮为选中的按钮                     (void)clickBtn:(UIButton *)button      //1.先将之前选中的按钮设置为未选中      self.selectedBtn.selected NO     //2.再将当前按钮设置为选中      button.selected YES     //3.最后把当前按钮赋值为之前选中的按钮      self.selectedBtn button;            //4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器)      self.selectedIndex button.tag;     @end  

 

XNTabBarButton.h

 

[objc] view plaincopy在CODE上查看代码片#import     @interface XNTabBarButton UIButton    @end   

 

XNTabBarButton.m

 

[objc] view%20plaincopy派生到我的代码片#import "XNTabBarButton.h"    @implementation XNTabBarButton    (void)setHighlighted:(BOOL)highlighted{  //    [super setHighlighted:highlighted];     @end
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表