自定义TabbarController, tabbarItem ,tabbar 屏幕适配
tabbarItem
.h文件
//
// JHKTabBarItem.h
// TestBaseView
//
// Created by 金英杰 on 2020/11/14.
//
@protocol JHKTabBarItemDelegate;
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface JHKTabBarItem : UIView
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) UIColor *titleColor;
@property (nonatomic, assign) id <JHKTabBarItemDelegate> delegate;
@end
@protocol JHKTabBarItemDelegate <NSObject>
- (void)tabBarItem:(JHKTabBarItem *)item didSelectIndex:(NSInteger)index;
@end
NS_ASSUME_NONNULL_END
.m文件
//
// JHKTabBarItem.m
// TestBaseView
//
// Created by 金英杰 on 2020/11/14.
//
#import "JHKTabBarItem.h"
#import "JHKTabBarItem.h"
static NSInteger defaultTag = 100000;
@interface JHKTabBarItem ()
@property (nonatomic, strong)UIImageView *iconImageView;
@property (nonatomic, strong)UILabel *titleLabel;
@end
@implementation JHKTabBarItem
- (instancetype)init {
self = [super init];
if (self) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemClicked:)];
[self addGestureRecognizer:tap];
}
return self;
}
// 重写setTag方法
- (void)setTag:(NSInteger)tag {
[super setTag:tag + defaultTag];
}
- (UIImageView *)iconImageView {
if (_iconImageView == nil) {
_iconImageView = [[UIImageView alloc]init];
_iconImageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:_iconImageView];
}
return _iconImageView;
}
- (UILabel *)titleLabel {
if (_titleLabel == nil) {
_titleLabel = [[UILabel alloc]init];
_titleLabel.textAlignment = NSTextAlignmentCenter;
_titleLabel.font = [UIFont systemFontOfSize:10];
_titleLabel.numberOfLines = 0;
_titleLabel.textColor = [UIColor grayColor];
[self addSubview:_titleLabel];
}
return _titleLabel;
}
- (void)setIcon:(NSString *)icon {
_icon = icon;
self.iconImageView.image = [UIImage imageNamed:icon];
}
- (void)setTitle:(NSString *)title {
_title = title;
self.titleLabel.text = title;
}
- (void)setTitleColor:(UIColor *)titleColor {
_titleColor = titleColor;
self.titleLabel.textColor = titleColor;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat space = 6.0;
if (self.icon.length > 0 && self.title.length > 0) {
CGFloat iconHeight = (CGRectGetHeight(self.frame)- jTabBarBottomHeight - space * 3)*2/3.0 ;
self.iconImageView.frame = CGRectMake(space, space, CGRectGetWidth(self.frame) - 2 * space, iconHeight);
self.titleLabel.frame = CGRectMake(space, CGRectGetMaxY(self.iconImageView.frame) + space, CGRectGetWidth(self.frame) - 2*space,iconHeight/2.0);
} else if (self.icon.length > 0) {
self.iconImageView.frame = CGRectMake(2, 2, CGRectGetWidth(self.frame