zoukankan      html  css  js  c++  java
  • iOS开发笔记_4自定义TabBar

       

    新博客:http://www.liuchendi.com

    好多APP都使用的是自定义的TabBar,那这个功能应该如何实现呢?首先应该解决的问题就是,加载NavigationController的时候,应该隐藏tabbar

    self.tabBar.hidden = YES;

      接着应该知道的时tabbar的高度是49,设置tabbar的背景

      

    //初始化定义tabbar背景
        UIImageView *tarBarView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
        tarBarView.userInteractionEnabled = YES;
        tarBarView.image = [UIImage imageNamed:@"tabbar_background"];
        [self.view addSubview:tarBarView];
        [tarBarView release];

      最后通过设置按钮,添加到tabbar上,这里设置了5个TabBarItem,可以选择边测试边调节item的位置

    //初始化定义tabbarItem
        float coordinax = 0;
        for (int index = 0 ; index <5 ; index ++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.tag = index;
            button.frame = CGRectMake(20+coordinax, 49.0/2-10, 25, 25);
            NSString *imageName = [NSString stringWithFormat:@"%d",index+1];
            [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
            
            [tarBarView addSubview:button];
            coordinax +=62;
        }

      最后把按钮触发的控制器链接起来,用selectedIndex属性

      

    -(void)changeViewController:(id) sender
    {
        UIButton *button = (UIButton *) sender;
        self.selectedIndex = button.tag;
    }

    总结:这里有个非常重要的地方就是tarBarView.userInteractionEnabled = YES 这里就把按钮给打开来了,如果不设置的话只是单纯的把按钮添加上去了,点击不了的,所以这里应该非常小心,其他的就没什么,都是只要逻辑写对了,问题不大。

     

  • 相关阅读:
    常见RGB透明度对照表在这
    Android节假日图标动态替换方案
    用两个栈实现队列
    从头到尾打印链表 (链表/栈)
    MySQL常用函数
    找出数组中重复的数字
    两数之和
    java的list的几种排序写法整理(sort的用法)
    Java知识目录
    通过关键词来推荐话题
  • 原文地址:https://www.cnblogs.com/iOS-dd/p/3154007.html
Copyright © 2011-2022 走看看