先来回顾一下UITabBarController
( 稍微详细的在在http://blog.csdn.net/yang198907/article/details/49807011)
伴随UITabBarController存在的一个控件叫做导航栏(UITabBar);
UITabBarController中有N个子控制器,那么在UITabBar中就会有N个UITabBarButton作为子控制器;

结构:


控制原理:1对应VC1,2对应VC2.....;



对应的UITabBarItem有相应的属性设置显示的内容,
eg:
标题文字
    @property(nonatomic,copy)NSString
*title;
     图标
    @property(nonatomic,retain)UIImage
*image;
     选中时的图标
    @property(nonatomic,retain)UIImage
*selectedImage;
     提醒数字
    @property(nonatomic,copy)NSString*badgeValue
典型的QQ案例:



什么时候需要自定义UITabBar
当UITabBarItem的默认功能显示不了我们的需求,或者说,我们想更加灵活的使用UITabBarItem的时候;
例如网易彩票:



注意️:文字和房子为一张图片
此时再去使用默认的UITabBarItem的属性设置就会有问题!
自定义UITabBar就派上用场了!


怎么实现自定义UITabBar?
先分析一下UITabBar的功能,点击UITabBarItem则会跳转到对应的控制器;
所以,我们只需要自定义一个UIView或者子类,然后再添加Button,点击Button时再跳转到对应的控制器就就可以了!

废话少说,上点代码:(代码实现了点击底部的button切换控制器)
步骤一:自定义UITabBarController
     1)创建YSCTabBarController类继承自UITabBarController
     2)创建
YSCTabBar类继承自
UIView,并添加3个Button模拟UITabBarItem;
   3)创建三个控制器继承自UIViewController

代码1:

            #import "YSCTabBarController.h"
#import "YSCTabBar.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h" @interface YSCTabBarController () <YSCTabBarDelegate> @end @implementation YSCTabBarController - (void)viewDidLoad {
[super viewDidLoad];
[self loadViewVC];
YSCTabBar *tabBar = [[YSCTabBar alloc] initWithFrame:self.tabBar.frame WithCount:self.viewControllers.count];
tabBar.delegate = self;
[self.view addSubview:tabBar];
} - (void)loadViewVC {
OneViewController *oneVC = [[OneViewController alloc] init];
TwoViewController *twoVC = [[TwoViewController alloc] init];
ThreeViewController *threeVC = [[ThreeViewController alloc] init];
self.viewControllers = @[oneVC,twoVC,threeVC];
} - (void)yscTabbar:(YSCTabBar *)tabar index:(NSInteger)index {
self.selectedIndex = index;
}
@end



 代码2:

           #import "YSCTabBar.h"
@implementation YSCTabBar
- (instancetype)initWithFrame:(CGRect)frame WithCount:(NSInteger )count {
if (self = [super initWithFrame:frame]) { CGFloat W = [UIScreen mainScreen].bounds.size.width / count;
CGFloat H = 49; for (int i = 0; i < count; i ++) {
CGFloat X = i * W; UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, W, H)];
btn.tag = i; btn.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
[self addSubview:btn]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
return self;
} - (void)btnClick:(UIButton *)btn{ if ([self.delegate respondsToSelector:@selector(yscTabbar:index:)]) {
[self.delegate yscTabbar:self index:btn.tag];
}
}
@end


      代码3、

            #import "OneViewController.h"
@interface OneViewController ()
@end
@implementation OneViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
@end


步骤二
        代码创建实例化UIWindow并创建
YSCTabBarController:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; YSCTabBarController *tabVC = [[YSCTabBarController alloc] init];
self.window.rootViewController = tabVC; [self.window makeKeyAndVisible];
return YES;
}

效果图:


总结:UITabBarController给我们提供了一个非常好的选择哪个控制的属性,否则,我们还需要根据不同的;
                         eg:
            - (void)yscTabbar:(YSCTabBar*)tabar
index:(NSInteger)index {

               
self.selectedIndex=
index;
            }
此外,子控件让父控件做一些事情的时候,可以通过代理或者block来实现,本demo使用的是代理的方式;


基于此,我们还可以写出创建YSCTabBar类继承自UIScrollView
实现如下效果:
 




iOS-自定义 UITabBarController的更多相关文章

  1. iOS 自定义UITabBarController的tabBar

               #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDeleg ...

  2. 【iOS自定义键盘及键盘切换】详解

    [iOS自定义键盘]详解 实现效果展示: 一.实现的协议方法代码 #import <UIKit/UIKit.h> //创建自定义键盘协议 @protocol XFG_KeyBoardDel ...

  3. iOS自定义的UISwitch按钮

    UISwitch开关控件 开关代替了点选框.开关是到目前为止用起来最简单的控件,不过仍然可以作一定程度的定制化. 一.创建 UISwitch* mySwitch = [[ UISwitchalloc] ...

  4. 如何实现 iOS 自定义状态栏

    给大家介绍如何实现 iOS 自定义状态栏 Sample Code: 01 UIWindow * statusWindow = [[UIWindow alloc] initWithFrame:[UIAp ...

  5. 自定义UITabbarController控制器

    自定义UITabbarController控制器 这是定制UITabbarController的基本原理,没有进行功能性封装. 效果:   源码地址: https://github.com/YouXi ...

  6. iOS自定义组与组之间的距离以及视图

    iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...

  7. iOS 自定义转场动画

    代码地址如下:http://www.demodashi.com/demo/12955.html 一.总效果 本文记录分享下自定义转场动画的实现方法,具体到动画效果:新浪微博图集浏览转场效果.手势过渡动 ...

  8. iOS 自定义转场动画浅谈

    代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...

  9. iOS自定义转场动画实战讲解

    iOS自定义转场动画实战讲解   转场动画这事,说简单也简单,可以通过presentViewController:animated:completion:和dismissViewControllerA ...

随机推荐

  1. spring中<context:property-placeholder/>一个坑

    <context:property-placeholder location="classpath:db.properties" ></context:prope ...

  2. java web基础

    WEB基础 C/S:即服务器-客服机(Client-Server)结构.C/S结构通常采用两层结构,服务器负责数据的管理,客户机负责完成与用户的交互任务.客户通过局域网与服务器相连,接受用户的请求,并 ...

  3. vue中生命周期

    1,说器生命周期,总觉得有熟悉,又陌生,直到看到一道面试题,问父子组件的生命周期的执行顺序,我擦,真没太注意啊,不知道. 2,网上搜了一下,说法是有点像洋葱圈的形式,由外到内,在到外,因为就像一个盒子 ...

  4. 【Mathtype】安装Mathtype后,word无法粘贴的问题

    Win10安装mathtype后,word工具栏中自动添加mathtype的选项(mathtype为了使用户更加方便使用,故自动添加),但是却导致word无法粘贴.如何解决该问题? [方案1]官网描述 ...

  5. php日志监控

    <?php date_default_timezone_set('Asia/Shanghai'); $time = date('Y-m-d H:i:s',time()); //访问时间 $ip ...

  6. Xilinx ISE多功能移位寄存器仿真及Basys2实验板实验

    移位寄存器实现Verilog代码: `timescale 1ns / 1ps module add( input clk, input reset, input [1:0] s, input dl, ...

  7. 性能测试之Docker监控

    微服务.大中台盛行的当下,容器化已经被广泛使用.在性能测试过程中,对容器的监控模型构建也是必不可少的. 我们性能测试监控模型的构建一直是围绕着Prometheus和Grafana来展开的.她们可以快速 ...

  8. Hystrix微服务容错处理及回调方法源码分析

    前言 在 SpringCloud 微服务项目中,我们有了 Eureka 做服务的注册中心,进行服务的注册于发现和服务治理.使得我们可以摒弃硬编码式的 ip:端口 + 映射路径 来发送请求.我们有了 F ...

  9. 学习源码的第八个月,我成了Spring的开源贡献者

    @ 目录 我的经历 碰到的问题 1.担心闹乌龙 2.不知道要怎么提交 3.英文 4.担心问题描述的不清楚 给你的建议 我的经历 关注我的朋友都知道,关注两个字划重点,要考! 我最近一直在写Spring ...

  10. Username for 'https://github.com': remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/GLSmile/pythontest.git/' 问题

    使用$ git push -u origin master 进行同步时,提示输入用户名和密码,但是我输入正确的信息后,仍然 会报Username for 'https://github.com': r ...