TabBar

作者: 失忆的程序员 | 来源:发表于2022-02-16 09:29 被阅读0次
//
//  XPFTabBar.h
//  YGVideoiOS
//
//  Created by apple on 2019/8/23.
//  Copyright © 2019 X. All rights reserved.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface XPFTabBar : UITabBar
///
@property (nonatomic, assign) NSInteger tabBarNumber;
///
@property (nonatomic, copy) void(^didMiddBtn)(void);

- (void)setTabbarBgColor:(UIColor *)bgColor index:(NSInteger)indexs;

@end

NS_ASSUME_NONNULL_END

//
//  XPFTabBar.m
//  YGVideoiOS
//
//  Created by apple on 2019/8/23.
//  Copyright © 2019 X. All rights reserved.
//

#import "XPFTabBar.h"

@interface XPFTabBar ()
///
@property (nonatomic, strong) UIButton *middleBtn;
///
@property (nonatomic, strong) UIButton *sendBtn;

@end

@implementation XPFTabBar


- (void)setTabbarBgColor:(UIColor *)bgColor index:(NSInteger)indexs
{
    if (@available(iOS 15.0, *))
    {
        
        UITabBarItemAppearance *itemAppearance = [[UITabBarItemAppearance alloc] init];
        //if ([title isEqualToString:@"首页"] || [title isEqualToString:@"我的"])
        if (indexs == 0 || indexs == 3)
        {
            itemAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor], NSFontAttributeName : YGFONT(15)};
            itemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : YGFONT(15)};
            itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -5.0*3.0);
            itemAppearance.selected.titlePositionAdjustment = UIOffsetMake(0, -5.0*3.0);
        }
        //else if ([title isEqualToString:@"直播"] || [title isEqualToString:@"课程"])
        else if (indexs == 1 || indexs == 2)
        {
            itemAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor] , NSFontAttributeName : YGFONT(15)};
            itemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : YGFONT(15)};
            itemAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -5.0*3.0);
            itemAppearance.selected.titlePositionAdjustment = UIOffsetMake(0, -5.0*3.0);
        }
        
        UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
        appearance.backgroundImage = [self imageWithColor:bgColor];
        appearance.backgroundEffect = nil;
        appearance.shadowImage = [self imageWithColor:[UIColor clearColor]];
        appearance.stackedLayoutAppearance = itemAppearance;
        self.standardAppearance = appearance;
        self.scrollEdgeAppearance = appearance;
        
         
    }
    else
    {

        if (@available(iOS 13.0, *))
        {
            // 该方法设置的是 UITabBar 的 Item 未选中颜色,这样选中的颜色就能正常显示了,
            [[UITabBar appearance] setUnselectedItemTintColor:[UIColor grayColor]]; // [UIColor colorWithHexString:@"#FFFFFF" alpha:0.6]
        }
        else
        {
            // [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]
            // 设置tabbar 选中文字的颜色
            [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : YGBOLDFONT(18), NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#FFFFFF"]} forState:UIControlStateSelected];

            // 设置tabbar  未选中文字的颜色
            [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : YGBOLDFONT(15), NSForegroundColorAttributeName : [UIColor colorWithHexString:@"#FFFFFF" alpha:0.6]} forState:UIControlStateNormal];
        }
        [self setBackgroundImage:[self imageWithColor:bgColor]];
        [self setShadowImage:[self imageWithColor:[UIColor clearColor]]];
    }
}

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


///
- (void)layoutSubviews {
    [super layoutSubviews];
    //
    [self addSubview:self.sendBtn]; 
    //
    [self.sendBtn setImagePositionWithType:SSImagePositionTypeBottom spacing:4]; //
    
    // 其他位置按钮
    NSUInteger count = self.subviews.count;
    for (NSUInteger i = 0 , j = 0; i < count; i++) {
        UIView *view = self.subviews[i];
        Class class = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:class]) {
            view.width = self.width / self.tabBarNumber;
            view.x = self.width * j / self.tabBarNumber;
            j++;
            if (j == 2) {
                j++;
            }
        }
    }
    
}
// 发布
- (void)didClickPublishBtn:(UIButton*)sender {
    if (self.didMiddBtn) {
        self.didMiddBtn();
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (self.isHidden == NO) {
        CGPoint newP = [self convertPoint:point toView:self.middleBtn];
        if ([self.middleBtn pointInside:newP withEvent:event]) {
            return self.middleBtn;
        } else {
            return [super hitTest:point withEvent:event];
        }
    } else {
        return [super hitTest:point withEvent:event];
    }
}

- (UIButton *)sendBtn {
    if (!_sendBtn) {
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setImage:[UIImage imageNamed:@"videoImg"] forState:UIControlStateNormal];
        //[_sendBtn setTitle:@"发布" forState:UIControlStateNormal];
        //_sendBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [_sendBtn addTarget:self action:@selector(didClickPublishBtn:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn.adjustsImageWhenHighlighted = NO;
        _sendBtn.size = CGSizeMake(self.bounds.size.width / self.tabBarNumber, 70);
        _sendBtn.centerX = self.width / 2;
        _sendBtn.centerY = 12;
        [_sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.middleBtn = _sendBtn;
    }
    return _sendBtn;
}

@end


UITabBarController 使用

//
    self.tabBar = [[XPFTabBar alloc] init];
    [self setValue:self.tabBar forKey:@"tabBar"];
    self.tabBar.tabBarNumber = self.titles.count + 1; // +1 的原因:别忘记添加中间的按钮
    [self.tabBar setDidMiddBtn:^{ // tab 可以点击 用一个值来控制重复点击造成的UI 混乱问题
        //
    }];

tabbar iOS 15 做了优化。

雪碧.gif

相关文章

网友评论

    本文标题:TabBar

    本文链接:https://www.haomeiwen.com/subject/lkbrlrtx.html