You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

153 lines
4.5 KiB

//
// MyTabBar.m
// CustomTabbarViewController
//
// Created by duanshengwu on 2019/7/11.
// Copyright © 2019 D-James. All rights reserved.
//
#import "MyTabBar.h"
#import "UIViewExt.h"
@interface MyTabBar ()
@property (strong, nonatomic) UIButton *centerTabBarBtn;
@property (strong, nonatomic) UILabel *titleLabel;
@end
@implementation MyTabBar
#define kBottomSafeSpace (iPhoneX ? 34.0 : 0.0)
- (instancetype)init{
if (self = [super init]){
[self initView];
}
return self;
}
- (void)initView{
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, -10, ScreenWidth, kBottomSafeSpace+80)];
// 加载图片
UIImage *image = [UIImage imageNamed:@"tabBar_background"];
// 设置不拉伸区域
CGFloat top = 25;
CGFloat left = 0;
CGFloat bottom = 0;
CGFloat right = 0;
// 拉伸图片
UIImage *bgImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right) resizingMode:UIImageResizingModeStretch];
UIImageView *imageView = [[UIImageView alloc] initWithImage:bgImage];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.frame = backView.frame;
[backView addSubview:imageView];
[self insertSubview:backView atIndex:0];
// 去除顶部横线
[self setBackgroundImage:[UIImage new]];
[self setShadowImage:[UIImage new]];
}
// layoutSubviews遍历子控件寻找UITabBarButton,给UITabBarButton重新设置frame
- (void)layoutSubviews {
[super layoutSubviews];
if (self.coutn > 0) return;
self.coutn = 1;
CGFloat width = 40;
self.centerTabBarBtn.frame = CGRectMake((ScreenWidth-width)/2, -8, width, width);
self.titleLabel.frame = CGRectMake(self.centerTabBarBtn.left, self.centerTabBarBtn.bottom+4, width, 10);
CGFloat tabBarButtonW = ScreenWidth / 5;
CGFloat tabBarButtonIndex = 0;
for (UIView *child in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([child isKindOfClass:class])
{
// 重新设置frame
CGRect frame = CGRectMake(tabBarButtonIndex * tabBarButtonW, 0, tabBarButtonW, 49);
child.frame = frame;
// // 增加索引
// if (tabBarButtonIndex == 1) {
// tabBarButtonIndex++;
// }
tabBarButtonIndex++;
}
}
}
////处理超出区域点击无效的问题
//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
// if (self.hidden){
// return [super hitTest:point withEvent:event];
// }else {
// //转换坐标
// CGPoint tempPoint = [self.centerTabBarBtn convertPoint:point fromView:self];
// //判断点击的点是否在按钮区域内
// if (CGRectContainsPoint(self.centerTabBarBtn.bounds, tempPoint)){
// //返回按钮
// return self.centerTabBarBtn;
// }else {
// return [super hitTest:point withEvent:event];
// }
// }
//}
- (void)setButtonSelectON:(BOOL)buttonSelectON
{
_buttonSelectON = buttonSelectON;
self.centerTabBarBtn.selected = buttonSelectON;
self.titleLabel.textColor = buttonSelectON ? mainColor : RGB(165, 165, 165);
}
- (void)centerTabBarBtnEvent:(UIButton *)btn
{
btn.selected = !btn.selected;
self.titleLabel.textColor = btn.selected ? mainColor : RGB(165, 165, 165);
if ([self.myTabBarDelegate respondsToSelector:@selector(tabbarClickButton:)])
{
[self.myTabBarDelegate tabbarClickButton:2];
}
NSLog(@"click event");
}
- (UIButton *)centerTabBarBtn {
if (_centerTabBarBtn == nil) {
_centerTabBarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_centerTabBarBtn.backgroundColor = KKClearColor;
[self addSubview:_centerTabBarBtn];
[_centerTabBarBtn setImage:[UIImage imageNamed:@"icon_location"] forState:UIControlStateNormal];
[_centerTabBarBtn setImage:[UIImage imageNamed:@"icon_location_select"] forState:UIControlStateSelected];
_centerTabBarBtn.adjustsImageWhenHighlighted = false;
[_centerTabBarBtn addTarget:self action:@selector(centerTabBarBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
}
return _centerTabBarBtn;
}
-(UILabel *)titleLabel
{
if (!_titleLabel)
{
_titleLabel = [UILabel new];
_titleLabel.text = @"定位";
_titleLabel.font = [UIFont systemFontOfSize:10];
_titleLabel.textColor = RGB(165, 165, 165);
_titleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:_titleLabel];
}
return _titleLabel;
}
@end