百思不得姐自定义TabBar

这里写图片描述

分析图片中中间Tabbar:
    可通过两个UIButton(选中和未选中)实现,之后就是通过layoutSubViews方法中重新摆放这个几个的位置

之后将语言转换为代码:

.h
#import <UIKit/UIKit.h>
@interface GCSTabBar : UITabBar
@end
.m

#import "GCSTabBar.h"
@interface GCSTabBar ()
/**发布按钮*/
@property (nonatomic, weak) UIButton *publishButton;

@end

@implementation GCSTabBar

/**
 *  http://www.jianshu.com/p/7e47da62899c
 */

/**
*  一般声明一些控件
*
*  @param frame frame description
*
*  @return return value description
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateSelected];
        [self addSubview:publishButton];
        self.publishButton = publishButton;
    }
    return self;
}


#pragma mark - 重新计算子控件位置

-(void)layoutSubviews {

    [super layoutSubviews];

    //设置发布的按钮,

    self.publishButton.frame = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
    //将自定义按钮放到中间
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);

    //设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;

        //计算按钮x的值,当是自定义tabbar则+1
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);

        //增加索引
        index++;
    }
}

@end

使用方式:

     //添加自定义TabBar
[self setValue:[[GCSTabBar alloc] init] forKeyPath:@"tabBar"];
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值