开发者社区> 问答> 正文

ios的tabbar问题求详细解答,这个tabbar下面的图标突出一块怎么做的

screenshot
这个tabbar下面的图标突出一块怎么做的

展开
收起
爵霸 2016-03-06 13:34:37 2471 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    自己自定义一个标签视图控制器吧:
    自定义一个类继承自UITabBarController;
    然后在该类的.m文件中自己写button和其点击事件间的切换

    #import "MyTabBarController.h"
    #define WIDTH (myView.frame.size.width / 4)
    #define HEIGHT (myView.frame.size.height)
    @interface MyTabBarController ()

    //之前选中的按钮
    @property(nonatomic, retain) UIButton *selectedBtn;
    @end
    @implementation MyTabBarController
    (void)viewDidLoad {
    [super viewDidLoad];
    //删除现有的tabBar
    CGRect rect = self.tabBar.frame;
    [self.tabBar removeFromSuperview];
    //添加自己的视图

    UIView *myView = [[BaseView alloc] init];
    myView.backgroundColor = [UIColor whiteColor];
    myView.frame = rect;
    [self.view addSubview:myView];

    //主页发现按钮

    UIButton *findBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [findBtn setImage:[UIImage imageNamed:@"icon_home_n"] forState:UIControlStateNormal];
    [findBtn setImage:[UIImage imageNamed:@"icon_home_h"] forState:UIControlStateSelected];
    findBtn.tag = 1;
    findBtn.frame = CGRectMake(0, -20, WIDTH, HEIGHT); //y设成负值,高再设高一点就好了!!!

    //默认选中是发现按钮

    findBtn.selected = YES;
    [findBtn setTintColor:[UIColor colorWithRed:0.886 green:0.365 blue:0.247 alpha:1.000]];
    self.selectedBtn = findBtn;
    [myView addSubview:findBtn];
    UILabel *findLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
    findLabel.text = @"发现";
    findLabel.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000];
    findLabel.font = [UIFont systemFontOfSize:10];
    findLabel.tag = 101;
    [myView addSubview:findLabel];
    [findLabel release];

    //搜索页

    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setImage:[UIImage imageNamed:@"icon_search_n"] forState:UIControlStateNormal];
    [searchBtn setImage:[UIImage imageNamed:@"icon_search_h"] forState:UIControlStateSelected];
    searchBtn.tag = 2;
    searchBtn.frame = CGRectMake(WIDTH, -20, WIDTH, HEIGHT);
    [myView addSubview:searchBtn];
    UILabel *searchLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
    searchLabel.text = @"搜索";
    searchLabel.textColor = [UIColor grayColor];
    searchLabel.font = [UIFont systemFontOfSize:10];
    searchLabel.tag = 102;
    [myView addSubview:searchLabel];
    [searchLabel release];

    //收藏页

    UIButton *costBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [costBtn setImage:[UIImage imageNamed:@"iconfont-like"] forState:UIControlStateNormal];
    [costBtn setImage:[UIImage imageNamed:@"iconfont-like-selected"] forState:UIControlStateSelected];
    costBtn.tag = 3;
    costBtn.frame = CGRectMake(2 * WIDTH, -20, WIDTH, HEIGHT);
    [myView addSubview:costBtn];
    UILabel *collectLabel = [[UILabel alloc] initWithFrame:CGRectMake(2 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
    collectLabel.text = @"收藏";
    collectLabel.textColor = [UIColor grayColor];
    collectLabel.font = [UIFont systemFontOfSize:10];
    collectLabel.tag = 103;
    [myView addSubview:collectLabel];
    [collectLabel release];

    //设置页

    UIButton *setUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setUpBtn setImage:[UIImage imageNamed:@"icon_setting_n"] forState:UIControlStateNormal];
    [setUpBtn setImage:[UIImage imageNamed:@"icon_setting_h"] forState:UIControlStateSelected];
    setUpBtn.tag = 4;
    setUpBtn.frame = CGRectMake(3 * WIDTH, -20, WIDTH, HEIGHT);
    [myView addSubview:setUpBtn];
    
    UILabel *setUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * WIDTH + WIDTH/2 -10, HEIGHT - 20, WIDTH, 20)];
    setUpLabel.text = @"设置";
    setUpLabel.textColor = [UIColor grayColor];
    setUpLabel.font = [UIFont systemFontOfSize:10];
    setUpLabel.tag = 104;
    [myView addSubview:setUpLabel];
    [setUpLabel release];
    
    for (int i = 1; i <4; i++) {
    UIButton *btn = [self.view viewWithTag:i];
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    }

    #pragma mark -- 自定义tabBar按钮的点击事件
    -(void)clickBtn:(UIButton *)sender {
    //设置上一个按钮

    self.selectedBtn.selected = NO;
    UILabel *titleFormer = [self.view viewWithTag:self.selectedBtn.tag + 100];
    titleFormer.textColor = [UIColor grayColor];
    
    //设置选中状态
    sender.selected = YES;
    self.selectedBtn = sender;
    UILabel *titleNow = [self.view viewWithTag:self.selectedBtn.tag + 100];
    titleNow.textColor = [UIColor colorWithRed:0.804 green:0.325 blue:0.227 alpha:1.000];
    
    //跳转相应的视图控制器
    self.selectedIndex = sender.tag - 1;
    }
    2019-07-17 18:54:28
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载