最近在新的項(xiàng)目中遇到的下面幾個(gè)問(wèn)題:
- 項(xiàng)目中需要實(shí)現(xiàn)類(lèi)似閑魚(yú)的發(fā)布凸出按鈕;
沒(méi)有自定義tabbar,只是子類(lèi)化了tabbar
1.設(shè)置imageInsets
// 設(shè)置圖片和文字之間的間
if ([title isEqualToString:@"發(fā)布"]) {
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子類(lèi)中設(shè)置背景圖片
- (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 的時(shí)候疑枯, 為中間按鈕削罩, 添加一個(gè)背景圖片
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.點(diǎn)擊范圍的設(shè)置
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.isHidden == NO) {
CGPoint newP = [self convertPoint:point toView:self.imageView];
//判斷如果這個(gè)新的點(diǎn)是在發(fā)布按鈕身上胀屿,那么處理點(diǎn)擊事件最合適的view就是發(fā)布按鈕
if ( [self.imageView pointInside:newP withEvent:event]) {
return self.btn;
}else{ //如果點(diǎn)不在發(fā)布按鈕身上,直接讓系統(tǒng)處理就可以了
return [super hitTest:point withEvent:event];
}
}
else { //tabbar隱藏了僻造,那么說(shuō)明已經(jīng)push到其他的頁(yè)面了憋他,這個(gè)時(shí)候還是讓系統(tǒng)去判斷最合適的view處理就好了
return [super hitTest:point withEvent:event];
}
}
- iOS13tarbbar默認(rèn)文字顏色無(wú)法設(shè)置
下面的代碼導(dǎo)致的
// 設(shè)置樣式, 去除tabbar上面的黑線
self.barStyle = UIBarStyleBlack;
1.設(shè)置文字顏色
// 設(shè)置 tabbarItem 選中狀態(tài)下的文字顏色(不被系統(tǒng)默認(rèn)渲染,顯示文字自定義顏色)
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.創(chuàng)建其他方法去除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 頁(yè)面跳轉(zhuǎn)后tabbar選擇顏色變成了默認(rèn)藍(lán)色
在tabbar子類(lèi)- (void)layoutSubviews方法中增加
self.tintColor =selectColor;