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. 005-shiro认证

    一.shiro认证流程 二.入门程序 1.代码: 2.配置shiro-first.ini 通过此配置文件创建securityManager工厂. 需要修改eclipse的ini的编辑器: 配置数据: ...

  2. JS的深拷贝

    var obj = { name: "wuyongyu", age: 18 } 第一种方式: function deepClone(obj){ // 判断传入的数据类型 - 数组或 ...

  3. HackerRank - string-reduction【反推】【规律】

    HackerRank - string-reduction[反推] 题意 给出一串 只有 字母 a, b, c 组成的字符串,然后没两个不同的字符碰到一起都可以变成另外一个字符,然后变到最后,求最短的 ...

  4. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary

    地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...

  5. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  6. Hadoop2.7.x中所有的DataNode都启动不了解决办法

    参考:Hadoop集群所有的DataNode都启动不了解决办法说明现象:我自己出现这个问题的原因是:自己在namenode格式化之后创建了一些文件,然后重新执行了namenode格式化导致的. 现象就 ...

  7. VSCode代码格式化自动换行问题

    打开VS设置,添加如下代码 "vetur.format.defaultFormatter.html": "js-beautify-html", "ve ...

  8. 【c++习题】【17/5/8】重载运算符

    1.设计一个Complex(复数)类,完成如下要求: 该类具有实部(Real_Part)和虚部(Image_Part)通过重载运算符“+”实现两个复数的相加通过重载运算符“+”实现一个复数与一个数值的 ...

  9. 响应式Tab选项卡

    在线演示 本地下载

  10. 解决Vim插入模式下backspace按键无法删除字符的问题【转】

    本文转载自:https://blog.csdn.net/zxy987872674/article/details/64124959 最近使用某个服务器编辑文件时,快捷键i进入插入模式后,下方不出现in ...