zoukankan      html  css  js  c++  java
  • AJ学IOS 之微博项目实战(4)微博自定义tabBar中间的添加按钮

    AJ分享,必须精品

    一:效果图

    自定义tabBar实现最下面中间的添加按钮
    这里写图片描述

    二:思路

    首先在自己的tabBarController中把系统的tabBar设置成自己的tabBar(NYTabBar),这里由于tabBar的属性是readonly的,所以我们要用kvc或者是消息管理来设置他

    然后就写自己的NYTabBar。这个写起来首先 遍历当前tabBar上的所有view,如果是UITabBarButton,就取出来重新设置他们的位置,并且重新赋值,接下来就是空出中间的添加的位置,然后把添加按钮放上去,就ok了。

    三:代码

    调用

    首先是如何调用的代码

        //自定义tabBar
        NYTabBar *tabBar = [[NYTabBar alloc]initWithFrame:self.tabBar.frame];
    
        //用kvc把readly的tabBar属性改成自定义的
        [self setValue:tabBar forKey:@"tabBar"];

    NYTabBar.m

    NYTabBar.h文件就不写了 ,里面啥都没有

    NYTabBar.m:

    //
    //  Created by apple on 15-7-24.
    //  Copyright (c) 2015年 znycat. All rights reserved.
    //
    
    #import "NYTabBar.h"
    
    
    @interface NYTabBar()
    /**
     *  添加增加按钮
     */
    @property (nonatomic, weak) UIButton *addButton;
    @end
    @implementation NYTabBar
    
    
    
    -(UIButton *)addButton
    {
        if (_addButton == nil) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
            [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    
            [btn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
            [btn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    
            _addButton = btn;
    
            //是按钮的尺寸默认跟背景图片一样大
            [btn sizeToFit];
    
            [self addSubview:_addButton];
    
        }
        return _addButton;
    }
    
    
    //调整子空间的位置
    -(void)layoutSubviews
    {
        [super layoutSubviews];
    
        CGFloat w = self.bounds.size.width;
        CGFloat h = self.bounds.size.height;
    
    
        CGFloat btnX = 0;
        CGFloat btnY = 0;
        CGFloat btnW = w / 5;
        CGFloat btnH = h;
    
        int i = 0;
    
        //1 , 遍历当前tabBar上的所有view
        for (UIView *tabBarBtn in self.subviews) {
            //2,如果是UITabBarButton,就取出来重新设置他们的位置
            if ([tabBarBtn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
    
                btnX = i * btnW;
    
                tabBarBtn.frame = CGRectMake(btnX, btnY, btnW, btnH);
    
                //当到了第二个时候,再加一个位置空竹添加按钮的位置。
                if (i==1) {
                    i++;
                }
    
                i++;
            }
        }
    
        //设置添加按钮 add按钮的位置
        self.addButton.center = CGPointMake(w * 0.5, h * 0.5);
    
    
    }
    
    @end
    

    四:补充

    调用的时候还可以用消息管理

        //自定义tabBar
        NYTabBar *tabBar = [[NYTabBar alloc]initWithFrame:self.tabBar.frame];
            /*这个地方还有一种用消息管理设置的,这样也可以,使用个步骤是
    
         1:先导入头文件#import <objc/message.h>
         2:到项目中Build Settings中查找msg ,然后把yes改成no
         3:代码写objc_msgSend调用就行了
            注意:这样的方法框架中比较多,目的为了不让人看懂,还有显得c牛逼,——————没啥鸟用。。
         */
         //用消息管理设置
       objc_msgSend(self, @selector(setTabBar:),tabBar);

    步骤:
    1:先导入头文件

    #import <objc/message.h>

    2:到项目中Build Settings中查找msg ,然后把yes改成no

    这里写图片描述

    3:代码写objc_msgSend调用就行了
    注意:这样的方法框架中比较多,目的为了不让人看懂,还有显得c牛逼,——————没啥鸟用。。

  • 相关阅读:
    StrCopy、StrCat、StrPas
    WinAPI: FlashWindow 闪烁窗口
    WinAPI: SetVolumeLabel 设置磁盘卷标
    WinAPI: GetActiveWindow 获取当前活动窗口的句柄
    WinAPI: SetCurrentDirectory、GetCurrentDirectory 设置与获取当前目录
    WinAPI: CreateDirectoryEx 根据模版建立文件夹
    WinAPI: CreateDirectory 建立文件夹
    WinAPI: RemoveDirectory 删除空目录
    WinAPI: GetLogicalDriveStrings 获取系统中存在的逻辑驱动器字符串
    filer.js: 一个 Unix 命令风格的 HTML 5 FileSystem API 封装 V2EX
  • 原文地址:https://www.cnblogs.com/luolianxi/p/4990304.html
Copyright © 2011-2022 走看看