最近在新的项目中遇到的下面几个问题:
- 项目中需要实现类似闲鱼的发布凸出按钮;
没有自定义tabbar,只是子类化了tabbar
1.设置imageInsets
// 设置图片和文字之间的间
if ([title isEqualToString:@"发布"]) {
controller.tabBarItem.imageInsets = UIEdgeInsetsMake(-20, 0, 10, 0);
controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
}else{
controller.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);
controller.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -2);
}
2.在tabbar子类中设置背景图片
- (void)layoutSubviews{
[super layoutSubviews];
Class class = NSClassFromString(@"UITabBarButton");
int btnIndex = 0;
for (UIView *btn in self.subviews){
if ([btn isKindOfClass:class]) {
if (btnIndex == 2) { // btnIndex == 2 的时候, 为中间按钮, 添加一个背景图片
self.imageView.frame = CGRectMake((btn.width-65)*0.5, -27, 65, btn.height + 10);//CGRectMake(5, -17, btn.lj_width - 10, btn.lj_height + 17)
[btn insertSubview:self.imageView atIndex:0];
self.btn = btn;
}
btnIndex++;
}
}
}
3.点击范围的设置
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.isHidden == NO) {
CGPoint newP = [self convertPoint:point toView:self.imageView];
//判断如果这个新的点是在发布按钮身上,那么处理点击事件最合适的view就是发布按钮
if ( [self.imageView pointInside:newP withEvent:event]) {
return self.btn;
}else{ //如果点不在发布按钮身上,直接让系统处理就可以了
return [super hitTest:point withEvent:event];
}
}
else { //tabbar隐藏了,那么说明已经push到其他的页面了,这个时候还是让系统去判断最合适的view处理就好了
return [super hitTest:point withEvent:event];
}
}
- iOS13tarbbar默认文字颜色无法设置
下面的代码导致的
// 设置样式, 去除tabbar上面的黑线
self.barStyle = UIBarStyleBlack;
1.设置文字颜色
// 设置 tabbarItem 选中状态下的文字颜色(不被系统默认渲染,显示文字自定义颜色)
NSDictionary *normalDic = [NSDictionary dictionaryWithObject:[AppConfigModel getColor:@"#999696"] forKey:NSForegroundColorAttributeName];
[controller.tabBarItem setTitleTextAttributes:normalDic forState:UIControlStateNormal];
NSDictionary *selectedDic = [NSDictionary dictionaryWithObject:[AppConfigModel obtainThemeColor] forKey:NSForegroundColorAttributeName];
[controller.tabBarItem setTitleTextAttributes:selectedDic forState:UIControlStateSelected];
2.创建其他方法去除tabbar上面的黑线
- (void)removiewTopline{
CGRect rect = CGRectMake(0, 0, [AppConfigModel obtainAllScreenWidth], [AppConfigModel obtainAllScreenHeight]);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, rect);
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self setShadowImage:img];
}
- iOS13 页面跳转后tabbar选择颜色变成了默认蓝色
在tabbar子类- (void)layoutSubviews方法中增加
self.tintColor =selectColor;