• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS自定义tabBar

武飞扬头像
Arnly
帮助1

iOS自定义tabBar

前段时间我们UI设计师设计了一个与系统自带样式的app的底部tabbar,它除了中间按钮要凸出来,整个tabbar的边部也是不贴边的。要做到这样的效果,就要对系统的babbar进行自定义。UITabBarController中有UITabBar这个类,这个类就是底部导航的关键控件类。

1、创建TextTabBar继承UITabBar

#import "TextTabBar.h"

#define SCREEN_WIDTH                [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT               [[UIScreen mainScreen] bounds].size.height

@interface TextTabBar ()

@property (nonatomic, strong) UIButton       *centerButton;
@property (nonatomic, strong) UIImageView    *tabbarBgImage;

@end

@implementation TextTabBar

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self) {
           [self addSubview:self.tabbarBgImage];
           // 去除顶部横线
            [self setBackgroundImage:[UIImage new]];
            [self setShadowImage:[UIImage new]];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 重设tabBar的位置
    self.frame = CGRectMake(0, SCREEN_HEIGHT- 67-10, SCREEN_WIDTH, 67);
    
    // 设置中间按钮
    CGFloat width = 72;
    self.centerButton.frame = CGRectMake(0 , -15, 45, 45);
    self.centerButton.center = CGPointMake(SCREEN_WIDTH/2, 15);
    
    
    // 设置其他tabbarbtn的frame
    CGFloat tabBarButtonW = (SCREEN_WIDTH-40)/5;
    CGFloat tabBarButtonIndex = 0;
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        if ([child isKindOfClass:class]) {
            CGRect frame = CGRectMake(tabBarButtonIndex * tabBarButtonW 20, 3, tabBarButtonW, 49);
            child.frame = frame;
            tabBarButtonIndex   ;
        }
    }
}


- (UIButton *)centerButton {
    if (_centerButton == nil) {
        _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_centerButton];
        _centerButton.backgroundColor = [UIColor redColor];
        _centerButton.layer.cornerRadius = 45/2;
        _centerButton.layer.masksToBounds = YES;
        _centerButton.adjustsImageWhenHighlighted = false;
        [_centerButton addTarget:self action:@selector(centerTabBarBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _centerButton;
}

- (UIImageView *)tabbarBgImage {
    if (_tabbarBgImage == nil) {
        _tabbarBgImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 67)];
        _tabbarBgImage.image = [UIImage imageNamed:@"home_tabbar_bg"];
    }
    return _tabbarBgImage;
}

// 中间按钮点击事件
- (void)centerTabBarBtnEvent{
    
}

学新通

2、 在UITabBarController中去重设tabbar这个属性

因为tabbar是属于UITabBarController的私有属性,所以我们不能用“.”语法对UITabBarController的tabbar直接设值,需要用KVC的"- (void)setValue:(nullable id)value forKey:(NSString *)key; " 方法对UITabBarController的tabbar设值。

  #import "TextTabBarController.h"
@interface TextTabBarController ()

@end

@implementation TextTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    TextTabBar *tabBar = [[TextTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
  }
  

3、给tabbar的添加控制器

#import "TextTabBarController.h"
#import "TextController.h"

@interface TextTabBarController ()

@end

@implementation TextTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    TextTabBar *tabBar = [[TextTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
    
    
    // 添加tabbar控制器
    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar1" image:@"home_tabbar_first" selectedImage:@"home_tabbar_first1"  tag:0];

    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar2" image:@"home_tabbar_travel" selectedImage:@"home_tabbar_travel1" tag:1];

   [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"" image:@"" selectedImage:@"" tag:2];
    
    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar3" image:@"home_tabbar_work" selectedImage:@"home_tabbar_work1" tag:3];

    [self setupOneChildViewController:[[UINavigationController alloc] initWithRootViewController:[[TextController alloc] init]] title:@"tabbar4" image:@"home_tabbar_mine" selectedImage:@"home_tabbar_mine1" tag:4];
    
}

/**
 *  初始化一个子控制器
 *
 *  @param vc            子控制器
 *  @param title         标题
 *  @param image         图标
 *  @param selectedImage 选中的图标
 */
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage tag:(NSInteger)tag
{
    vc.tabBarItem.title = title;
    vc.tabBarItem.tag = tag;
    if (image.length) { // 图片名有具体值
        vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
    }
    
    [self addChildViewController:vc];
}
学新通

效果

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcbkbg
系列文章
更多 icon
同类精品
更多 icon
继续加载