先来回顾一下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:

  1. #import "YSCTabBarController.h"
  2. #import "YSCTabBar.h"
  3. #import "OneViewController.h"
  4. #import "TwoViewController.h"
  5. #import "ThreeViewController.h"
  6. @interface YSCTabBarController () <YSCTabBarDelegate>
  7. @end
  8. @implementation YSCTabBarController
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. [self loadViewVC];
  12. YSCTabBar *tabBar = [[YSCTabBar alloc] initWithFrame:self.tabBar.frame WithCount:self.viewControllers.count];
  13. tabBar.delegate = self;
  14. [self.view addSubview:tabBar];
  15. }
  16. - (void)loadViewVC {
  17. OneViewController *oneVC = [[OneViewController alloc] init];
  18. TwoViewController *twoVC = [[TwoViewController alloc] init];
  19. ThreeViewController *threeVC = [[ThreeViewController alloc] init];
  20. self.viewControllers = @[oneVC,twoVC,threeVC];
  21. }
  22. - (void)yscTabbar:(YSCTabBar *)tabar index:(NSInteger)index {
  23. self.selectedIndex = index;
  24. }
  25. @end



 代码2:

  1. #import "YSCTabBar.h"
  2. @implementation YSCTabBar
  3. - (instancetype)initWithFrame:(CGRect)frame WithCount:(NSInteger )count {
  4. if (self = [super initWithFrame:frame]) {
  5. CGFloat W = [UIScreen mainScreen].bounds.size.width / count;
  6. CGFloat H = 49;
  7. for (int i = 0; i < count; i ++) {
  8. CGFloat X = i * W;
  9. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, W, H)];
  10. btn.tag = i;
  11. 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];
  12. [self addSubview:btn];
  13. [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  14. }
  15. }
  16. return self;
  17. }
  18. - (void)btnClick:(UIButton *)btn{
  19. if ([self.delegate respondsToSelector:@selector(yscTabbar:index:)]) {
  20. [self.delegate yscTabbar:self index:btn.tag];
  21. }
  22. }
  23. @end


      代码3、

  1. #import "OneViewController.h"
  2. @interface OneViewController ()
  3. @end
  4. @implementation OneViewController
  5. - (void)viewDidLoad {
  6. [super viewDidLoad];
  7. // Do any additional setup after loading the view.
  8. self.view.backgroundColor = [UIColor redColor];
  9. }
  10. @end


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

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

效果图:


总结: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. python re 里面match 和search的区别

    re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. 例子: >>> re.match("c", &q ...

  2. Docker 部署Spring Boot 项目并连接mysql、redis容器(记录过程)

    Spring Boot 项目配置 将写好的Spring Boot 项目通过maven 进行package打包获得可执行Jar 再src/main/docker(放哪都行)下编写创建Dockerfile ...

  3. place-holder样式

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #777; } input:-moz-pl ...

  4. js 获取table tr td内的select 和input text

    $("#TableList tr").each(function () {                //for (var i = 1; i <= AM_index; i ...

  5. Java 解决采集UTF-8网页空格变成问号乱码

    http://blog.csdn.net/bob007/article/details/27098875 使用此方法转换后,在列表中看到的正常,但是在详情页的文本框中查看到的就是 了,只好过滤掉所有的 ...

  6. springboot使用redis的keyspace notifications 实现定时通知

    简单定时任务解决方案:使用redis的keyspace notifications(键失效后通知事件) 需要注意此功能是在redis 2.8版本以后推出的,因此你服务器上的reids最少要是2.8版本 ...

  7. SpringBoot系列——状态机(附完整源码)

    1. 简单介绍状态机 2. 状态机的本质 3. 状态机应用场景 1. 简单介绍状态机 状态机由状态寄存器和组合逻辑电路构成,能够根据控制信号按照预先设定的状态进行状态转移,是协调相关信号动作.完成特定 ...

  8. js 获取百度搜索关键词的代码

    有可能有时候我们会用到在百度搜什么关键词进来我们的网站的,所有我们又想拿到用户搜索的关键词. 这是我研究了半天所得出的办法.话不多说直接贴代码 <script> function quer ...

  9. let面试题

    两次输出结构都是2 0 1

  10. vue仿移动端输入框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...