iOS 自定义TabBar的实现

实现效果大概就是这个样子的,图片不咋好看,凑活着看看

直接贴代码吧

  1. 首先创建一个singleview程序
  2. 将ViewController基类改成UITabBarController
  3. 然后写.m文件 如下[ps  我这是开了arc的]
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self initOwnControllers];
    [self initOwnTabView];
    [self.tabBar setHidden:YES];
}

/*
 *设置多个标签页,为了切换
 **/
- (void)initOwnControllers{
    NSArray *array = @[@"主页",@"更多",@"文件",@"信息",@"发现"];  // 每个标签页标题
    
    NSMutableArray *conArray = [[NSMutableArray alloc] init];
    
    for (NSString *object in array) {  // 遍历
        UIViewController *con = [[UIViewController alloc] init];
        con.title = object;
        // 设置导航栏是为了显示标题,便于区分切换到哪页
        UINavigationController *controll = [[UINavigationController alloc] initWithRootViewController:con];
        [conArray addObject:controll];
    }
    
    self.viewControllers = conArray;
}

/*
 * 定义自己的tabbar
 **/
- (void)initOwnTabView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 519, 310, 44)]; // 使用最新的XCode,坐标有点问题,自己调一下就好
    view.backgroundColor = [UIColor whiteColor];
    view.layer.cornerRadius = 9; // 设置圆角,好看点
    
    // 加载每个按钮的图片
    NSArray *imageArray = @[@"home.png",@"much.png",@"file.png",@"message.png",@"discover.png"];
    for (int index = 0; index < [imageArray count]; index++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setImage:[UIImage imageNamed:imageArray[index]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventTouchDown];
        button.frame = CGRectMake(21+index*62, 7, 30, 30);  // 这个坐标要设置正确
        
        button.tag = index;   // 设置tag 为了方便设置切换页
        [view addSubview:button];
    }
    
    [self.view addSubview:view];
}

- (void)pageChanged:(UIButton *)button{
    self.selectedIndex = button.tag;   // 自己的按钮的事件处理
}
时间: 2024-08-25 12:53:02

iOS 自定义TabBar的实现的相关文章

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法

iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar  的解决办法 问题:iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 1.自定义代码: - (void)viewWillAppear:(BOOL)animated { [super  viewWillAppear:animated]; // 删除系统自动生成的UITabBarB

iOS 自定义tabbar 关于push问题 小技巧

在开发的时候,相信大家都用过tabbar ,今天我在写项目的时候也用到了tabbar  紧着着一系列问题就来了 需求:我的项目的主要框架是tabbar ,但是用系统的tabbar不美观 于是我就自定义了我的tabbar,创建了一个类,继承自UITabBarController,在这个类中我将系统原生的tabbar隐藏了,就写了这样一句话, self.tabBar.hidden=YES; 然后就是一系列的我们写得不能在熟的代码了,引入viewcontroller的头文件,然后实例化,将viewco

IOS 自定义TabBar选中时为蓝色解决方法

- (id) initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedImage 使用该方法设置会图片选中时会变成蓝色的. 如果不希望成为蓝色需改变图片的属性为UIImageRenderingModeAlwaysOriginal具体代码如下 [self setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwa

仿制新浪微博iOS客户端之三-自定义TabBar

继续上一篇文章的进度,我们实际完成了微博基本框架的搭建,具体实现的效果如下左图,但我们实际需要实现的效果为右图,除去主要的页面内容不谈,仅仅下面的TabBar距离我们的需求就有相当的差距.因此本文着重于实现需要的效果.                           再简要汇总一下我们的需求: 1.我们要在TabBar原有四个按钮的基础上,再增加一个按钮,作为撰写微博的入口: 2.新加入的按钮必须和原有按钮一起,均匀分布在TabBar上: 3.新加入的按钮只有图片,没有文字. 需求汇总如上,

Android UI之自定义——类似iOS的Tabbar

Android UI之自定义--类似iOS的Tabbar Tabbar最早出现在iOS,iOS中的TabBarController实现了这个功能,开发起来相当简单.现在的APP,大多数都会使用Tabbar来作为应用的功能导航,界面简单清晰.那么Android常见的实现是通过RadioGroup来实现,今天将带来自定义实现,补充RadioGroup实现的不足. 先看看常见的软件中的使用: 这个是高铁管家APP,大家应该非常熟悉.这个APP的首页底部就是一个类似iOS的Tabbar.这里就不多举例子

ios中解决自定义tabbar跳转隐藏问题的方法

在ios开发(http://www.maiziedu.com/course/ios/)中,如何自定义tabbar高度的跳转隐藏问题,比如和系统自带的tabbar高度不一样导致的有一条线的问题,还有push时动画效果等等一些列问题不在这里累述了,当然,思路有很多,可以参考以上链接自己琢磨琢磨,好了,下面直接上个人认为完美解决办法. 需求 1.自定义tabbar,不用系统的tabbar 2.第二点需求是自定义tabbar的高度和系统的不一样,系统的tabbar高度为49,就是因为这点导致第三个需求有

IOS开发-关于自定义TabBar条

今天在做项目的时候,突然有一个模块需要自定义TabBar条. 在平常很多做项目的时候,都没有去自定义过,大部分都是使用系统自带的.今天整理一个自定义TabBar条的步骤. 首先看下我们最终实现的效果: 首先需要继承UItabBar自定义一个自己的tabBar .h #import <UIKit/UIKit.h> @class THTabBar; @protocol THTabBarDelegate <UITabBarDelegate> @optional - (void)tabBa

iOS开发之功能模块--关于自定义TabBar条

只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. 1 #import <UIKit/UIKit.h> 2 3 @interface BikeTabBar : UITabBar 4 5 @end 1 #import "BikeTabBar.h" 2 3 @interface BikeTabBar () 4 5 //@property (nonatomic,weak)UIButton *centerButton; 6 7 @en

IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearchBar *searchBar = [HWSearchBar searchBar]; searchBar.width = 300; searchBar.height = 30; self.navigationItem.titleView = searchBar; //设置titleView 是搜索框