MyCustomTabBar.h文件

#import <UIKit/UIKit.h>

@interface MyCustomTabBar : UITabBarController

@end

MyCustomTabBar.m文件

#import "MyCustomTabBar.h"

#define kDeviceHeigh [UIScreen mainScreen].bounds.size.height

@interface MyCustomTabBar ()<UITabBarDelegate>

@end

@implementation MyCustomTabBar

-(void)viewDidLoad{

[super viewDidLoad ];

//自定义TabBar

UITabBar *tabBar = [[UITabBar alloc]initWithFrame:self.tabBar.frame];

tabBar.userInteractionEnabled = YES;

[self.view addSubview:tabBar];

tabBar.backgroundImage = [UIImage imageNamed:@"tabbg"];

// 添加5个

for (int i = 1 ; i <= 5 ; i ++ ) {

// 获取按钮普通状态和选中状态的两个图片的名称

NSString *imgName = [NSString stringWithFormat:@"TabBar%d", i];

NSString *selImgName = [NSString stringWithFormat:@"TabBar%dSel", i];

// 创建按钮

UIButton *tabBtn = [[UIButton alloc]init ];

CGFloat width = self.view.bounds.size.width / 5;

// 设置按钮的frame

tabBtn.frame =CGRectMake((i-1) * width , 0 , width , 44);

if ( i == 3 ) {

tabBtn.frame = CGRectMake(tabBtn.frame.origin.x , tabBtn.frame.origin.y - 20, tabBtn.frame.size.width, 44 + 20 );

[tabBtn setBackgroundImage:[UIImage imageNamed:@"logoff_btn_n"] forState:UIControlStateNormal];

}else{

// 设置按钮普通状态和选中状态显示的图片

[tabBtn setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];

[tabBtn setBackgroundImage:[UIImage imageNamed:selImgName] forState:UIControlStateSelected ];

}

tabBtn.tag = 10 + i;

[tabBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

[tabBar addSubview:tabBtn];

}

}

-(void)btnClicked:(UIButton *)button{

for (int i = 1  ; i <= 5 ; i++ ) {

UIButton *btn = (UIButton *)  [ self.view viewWithTag:10 + i];

if (btn != button) {

btn.selected = NO;

}

}

button.selected = YES;

if (button.tag == 13) {

UIViewController *vc = [[UIViewController alloc]init];

vc.view.backgroundColor = [UIColor orangeColor];

[self presentViewController:vc animated:YES completion:nil];

return;

}

self.selectedIndex = button.tag - 11;

}

@end

AppDelegate.m文件

#import "AppDelegate.h"

#import "MyCustomTabBar.h"

#import "FifthVC.h"

#import "SecondVc.h"

#import "ThirdVC.h"

#import "FourthVC.h"

#import "FirstVC.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

//先创建五个控制器对象

FirstVC *firstVC = [[FirstVC alloc]init];

SecondVc  *secondVC = [[SecondVc alloc] init];

ThirdVC *thirdVC = [[ThirdVC alloc]init];

FourthVC *fourthVC = [[FourthVC alloc]init];

FifthVC *fifthVC = [[FifthVC alloc]init];

// 创建TabBar控制器对象

MyCustomTabBar *tabBarVC = [[MyCustomTabBar alloc]init];

// 设置Tabbar的子控制器对象, 需要注意的是,TabBar控制器最多只能显示五个子视图,如果超过5个,它会只显示4个,把其他的放More选项中

tabBarVC.viewControllers = @[firstVC, secondVC, thirdVC, fourthVC, fifthVC];

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

self.window.rootViewController = tabBarVC;

[self.window makeKeyAndVisible];

return YES;

}

UI-自定义TabBar的更多相关文章

  1. iOS框架搭建(MVC,自定义TabBar)--微博搭建为例

    项目搭建 1.新建一个微博的项目,去掉屏幕旋转 2.设置屏幕方向-->只有竖向 3.使用代码构建UI,不使用storyboard 4.配置图标AppIcon和LaunchImage 将微博资料的 ...

  2. flutter 自定义tabbar 给tabbar添加背景功能

    flutter 自带的tabbar BottomNavigationBar有长按水波纹效果,不可以添加背景图片功能,如果有这方面的需求,就需要自定义tabbar了 自定义图片 我们使用BottomAp ...

  3. 自定义tabBar

    ★★★★自定义tabBar★★★★★★★ Demo下载地址:https://github.com/marlonxlj/tabBarCustom.git 前言: 有的时候需求要对tabBar进行自定义的 ...

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

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  5. 原生HTML5 input type=file按钮UI自定义

    原生<input type="file" name="file" />长得太丑 提升一下颜值 实现方案一.设置input[type=file]透明度 ...

  6. iOS 隐藏自定义tabbar

    iOS  隐藏自定义tabbar -(void)viewWillAppear:(BOOL)animated { NSArray *array=self.tabBarController.view.su ...

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

    只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...

  8. iOS开发项目之四 [ 调整自定义tabbar的位置与加号按钮的位置]

    自定义tabbar与按钮的添加 01 - 把系统的tabbar用我们自己的覆盖 LHQTabBar *lhqTabBar = [[LHQTabBar alloc]init]; [self setVal ...

  9. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

  10. 第二篇、Swift_自定义 tabbar 的 badgeValue显示样式

    在实际的开发中,我们常常需要根据实际的需求,去改变bageValue的显示样式,默认是红色的背景,白色的字体颜色 使用方式: class BKTabBarController: UITabBarCon ...

随机推荐

  1. keras中 LSTM 的 [samples, time_steps, features] 最终解释

    I am going through the following blog on LSTM neural network:http://machinelearningmastery.com/under ...

  2. mysql增量恢复的一个实例操作

    通过防火墙禁止web等应用向主库写数据或者锁表,让主库暂时停止更新,然后进行恢复 模拟整个场景 1.登录数据库 [root@promote 3306]# mysql -uroot -S /data/3 ...

  3. Rest_framework-1

    目录 一.认证 二.权限 三.限制访问频率 四.总结 一.认证(补充) 认证请求头 #!/usr/bin/env python # -*- coding:utf-8 -*- from rest_fra ...

  4. s5_day3作业

    # 一.元素分类 # 有如下值集合[11, 22, 33, 44, 55, 66, 77, 88, 99, 90...],将所有大于 # # 的值保存至字典的第一个key中,将小于 # # 的值保存至 ...

  5. Java乐观锁的实现原理(案例)

    简要说明: 表设计时,需要往表里加一个version字段.每次查询时,查出带有version的数据记录,更新数据时,判断数据库里对应id的记录的version是否和查出的version相同.若相同,则 ...

  6. java 工厂模式的作用,为什么要用工厂模式以及示例演示

    1 工厂模式的作用,为什么要用工厂模式? 工厂模式是为了解耦:把对象的创建和使用的过程分开.就是Class A 想调用Class B,那么只是调用B的方法,而至于B的实例化,就交给工厂类. 工厂模式可 ...

  7. java 怎么让打印信息换行?

    System.out.println("账号=="+name+"\n"); System.out.println("密码=="+pwd+&q ...

  8. HDFS JAVA API介绍

    注:在工程pom.xml 所在目录,cmd中运行 mvn package ,打包可能会有两个jar,名字较长的是包含所有依赖的重量级的jar,可以在linux中使用 java -cp 命令来跑.名字较 ...

  9. oracle 11g的卸载

    oracle 11g 的卸载主要有两种方式:一种是使用Oracle Universal Installer管理工具,该工具以向导模式进行,比较简单.这里主要讲解第二种卸载数据库的方式-----使用”d ...

  10. CSS3小图标菜单导航

    在线演示 本地下载