//
//  MyTabbar.m
//  Huangshan
//
//  Created by xiejiangbo on 2018/12/29.
//  Copyright © 2018 yin chen. All rights reserved.
//

#import "MyTabbar.h"

@interface MyTabbar()
/** 按钮 */
@property (nonatomic, weak) UIButton * publishBtn;

@end

@implementation MyTabbar
/**
 * 初始化
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        // 设置tabbar的子控件
        UIButton * publishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishBtn setBackgroundImage:[UIImage imageNamed:@"home_image"] forState:UIControlStateNormal];
        [publishBtn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [publishBtn sizeToFit];
        [self addSubview:publishBtn];
        self.publishBtn = publishBtn;
        
    }
    return self;
}
-(void) action:(UIButton *)sender{
    if (self.actionBlock != nil) {
        self.actionBlock();
    }
}
/**
 * 重写布局子控件的方法进行布局
 */
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat width = self.frame.size.width;
    CGFloat height = 49;
    // 设置发布按钮的frame
    self.publishBtn.center = CGPointMake(width * 0.5, (height * 0.5)-7);

    // 设置其他的UITabBar的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = width /5;
    CGFloat buttonH = height;
    NSInteger index = 0;
    NSLog(@"%lu",self.items.count);
    
    
    for (UIButton  * button in self.subviews) {
        // 判断 - 只有是UITabBarButton的button才进行布局（也就是tabbar上面的精华、新帖、关注、我这四个按钮） 而发布按钮不是UITabBarButton，就不进行布局
        // 由于UITabBar是苹果官方私有的， 所以不能直接设
        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")])  continue;
        if (button.tag == 0) {
            button.frame = CGRectMake(index *buttonW, buttonY, buttonW, buttonH);
            // 索引增加
            index ++;
            if (index == 2) {
                index ++;
            }
            button.tag = 100+index;
        }else if (button.tag != 0){
            if (button.tag == 103) {
                button.frame = CGRectMake((button.tag-102) *buttonW, buttonY, buttonW, buttonH);
            }else {
                button.frame = CGRectMake((button.tag-101) *buttonW, buttonY, buttonW, buttonH);
            }
            
        }
    }
}

@end
