最近在新的項目中遇到的下面幾個問題:
- 項目中需要實現類似閑魚的發布凸出按鈕;
沒有自定義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;