使用RDRDVTabBarController第三方方法,如何实现中间tab突出显示的效果

实现效果如下:


实现逻辑主要是在RDVTabBarController方法中添加一个UIButton,来遮住原来的TabItem

#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import <objc/runtime.h>

@interface UIViewController (RDVTabBarControllerItemInternal)

- (void)rdv_setTabBarController:(RDVTabBarController *)tabBarController;

@end

@interface RDVTabBarController () {
    UIView *_contentView;
    UIButton *_centerBtn;
    CGRect _frameBtn;
}

@property (nonatomic, readwrite) RDVTabBar *tabBar;

@end

@implementation RDVTabBarController

#pragma mark - View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:[self contentView]];
    [self.view addSubview:[self tabBar]];
    
    NSUInteger index = self.viewControllers.count/2;
    RDVTabBarItem *item = self.tabBar.items[index];
    
    UIImage *image = item.finishedSelectedImage;
    CGSize imageSize = [image size];
    imageSize = CGSizeMake(imageSize.width/3*2, imageSize.height/3*2);
    
    CGRect frame = [UIScreen mainScreen].bounds;
    float width = frame.size.width/(self.viewControllers.count-1);
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake((frame.size.width-width)/2, frame.size.height-width, width, width);
    btn.backgroundColor = KTabBarBackgroundColor;
    btn.layer.cornerRadius = width/2;
    btn.layer.borderWidth = 2;
    btn.layer.borderColor = HEX_COLOR(@"d0d0d8").CGColor;
    btn.adjustsImageWhenDisabled = NO;
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTintColor:KUnSelectedTitleAttributes];
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
    [btn setImageEdgeInsets:UIEdgeInsetsMake((width-imageSize.height)/2-10, (width-imageSize.width)/2, (width-imageSize.width)/2+10, (width-imageSize.width)/2)];
    if(WIDTH == 320){
        btn.titleLabel.font = [UIFont systemFontOfSize:11.0f];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(28, -30, 0, 5)];
    }else{
        btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(35, -33, 0, 5)];
    }
    btn.tag = index;
    [btn addTarget:self action:@selector(clickCenterBtn:) forControlEvents:UIControlEventTouchUpInside];
    _centerBtn = btn;
    [_centerBtn setAdjustsImageWhenHighlighted:NO];
    <span style="font-family: Arial, Helvetica, sans-serif;">[self.view addSubview:_centerBtn];</span>
    _frameBtn = _centerBtn.frame;
}

- (IBAction)clickCenterBtn:(UIButton*)sender{
    
    if ([[self delegate] respondsToSelector:@selector(tabBarController:shouldSelectViewController:)]) {
        if (![[self delegate] tabBarController:self shouldSelectViewController:[self viewControllers][sender.tag]]) {
            return;
        }
    }
    
    if ([self selectedViewController] == [self viewControllers][sender.tag]) {
        if ([[self selectedViewController] isKindOfClass:[UINavigationController class]]) {
            UINavigationController *selectedController = (UINavigationController *)[self selectedViewController];
            
            if ([selectedController topViewController] != [selectedController viewControllers][0]) {
                [selectedController popToRootViewControllerAnimated:YES];
            }
        }
        
        return;
    }
    
    
    [self setSelectedIndex:sender.tag];
    if ([[self delegate] respondsToSelector:@selector(tabBarController:didSelectViewController:)]) {
        [[self delegate] tabBarController:self didSelectViewController:[self viewControllers][sender.tag]];
    }
    [sender setTintColor:KSelectedTitleAttributes];
}

隐藏显示tabbar

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
    _tabBarHidden = hidden;
    
    __weak RDVTabBarController *weakSelf = self;
    
#ifdef HMC
    //上下出现
    void (^block)() = ^{
        CGSize viewSize = weakSelf.view.bounds.size;
        CGFloat tabBarStartingY = viewSize.height;
        CGFloat contentViewHeight = viewSize.height;
        CGFloat tabBarHeight = CGRectGetHeight([[weakSelf tabBar] frame]);
        
        if (!tabBarHeight) {
            tabBarHeight = 49;
        }
        
        if (!hidden) {
            tabBarStartingY = viewSize.height - tabBarHeight;
            if (![[weakSelf tabBar] isTranslucent]) {
                contentViewHeight -= ([[weakSelf tabBar] minimumContentHeight] ?: tabBarHeight);
            }
            [[weakSelf tabBar] setHidden:NO];
        }
        
        [[weakSelf tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
        [[weakSelf contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];
    };
#else
    //水平出现
    void (^block)() = ^{
        CGRect frame = [[weakSelf tabBar] frame];
        frame.origin.y = HEIGHT - 49;
        frame.size = CGSizeMake(WIDTH, 49);
        if(!hidden){
            _centerBtn.frame = _frameBtn;
            frame.origin.x = 0;
            [[weakSelf tabBar] setHidden:NO];
        }else{
            CGRect frameBtn = _frameBtn;
            frameBtn.origin.x -= WIDTH;
            _centerBtn.frame = frameBtn;
            frame.origin.x = -WIDTH;
        }
        [[weakSelf tabBar] setFrame:frame];
        [[weakSelf contentView] setFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    };
#endif
    void (^completion)(BOOL) = ^(BOOL finished){
        if (hidden) {
            [[weakSelf tabBar] setHidden:YES];
        }
    };
    
    if (animated) {
        [UIView animateWithDuration:0.24 animations:block completion:completion];
    } else {
        block();
        completion(YES);
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值