項目有個功能起宽,向后臺請求數(shù)據(jù)后如果有活動會在原有的TabbarController的Tabbar中間添加一個稍微大點的按鈕,由四個Item變?yōu)槲鍌€瘫俊,因為點擊中間的按鈕根據(jù)后臺數(shù)據(jù)分為兩種情況:一種相當(dāng)于直接切換了TabbarController的index,另一種是先不切換index,在原來的ViewController上面Present出來一個半透明的蒙版鹊杖,蒙版上面有兩個Button,點擊button后再切換index.效果如下:
所以想著將四個Item變?yōu)槲鍌€以后在中間添加一個按鈕覆蓋中間的TabBarItem,雖然功能已經(jīng)實現(xiàn)了扛芽,但是中間的按鈕并沒有完全覆蓋一個Item骂蓖,所以如果點擊到按鈕周圍還是有可能觸發(fā)被覆蓋的TabBarItem,不(鉆)能(牛)容(角)忍(尖)川尖!登下,然后在自己繼承重寫的TabbarController中設(shè)置一個屬性判斷是否是點擊自定義的button觸發(fā)的代理方法(centerBtnClicked:方法中設(shè)置了self.selectedIndex = 2,會觸發(fā)代理方法 shouldSelectedViewController:),具體實現(xiàn)代碼如下:
#import "YYFTabBarController.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
@interface YYFTabBarController ()<UITabBarControllerDelegate,UIGestureRecognizerDelegate>
{
BOOL _isCenterBtnPressed;
}
@property (strong, nonatomic) UITapGestureRecognizer *tap;
@property (strong, nonatomic) UIView *centerView;
@end
@implementation YYFTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
// Do any additional setup after loading the view.
}
-(void)setShowCenterBtn:(BOOL)showCenterBtn{
if (showCenterBtn==YES) {
[self addCenterBtn];
}
_showCenterBtn = showCenterBtn;
}
//add center button
-(void)addCenterBtn{
self.centerView = [[UIView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-60)/2, -(60-49)/2.0, 60.0f, 60.0f)];
self.centerView.backgroundColor = FANLITOU_RED_COLOR;
self.centerView.layer.cornerRadius = 30.0f;
UIImageView *menuLogo = [[UIImageView alloc]initWithFrame:CGRectMake(20, 15, 20, 20)];
[menuLogo sd_setImageWithURL:[NSURL URLWithString:self.activityDic[@"menu_logo"]]];
UILabel *menuName = [[UILabel alloc]initWithFrame:CGRectMake(0, 37, 60, 20)];
menuName.textAlignment = NSTextAlignmentCenter;
menuName.text = self.activityDic[@"menu_name"];
menuName.font = [UIFont systemFontOfSize:10];
menuName.textColor = [UIColor whiteColor];
[ self.centerView addSubview:menuLogo];
[ self.centerView addSubview:menuName];
self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(centerBtnClicked:)];
self.tap.delegate = self;
//add tap gesture
[ self.centerView addGestureRecognizer:self.tap];
[self.tabBar addSubview: self.centerView];
}
-(void)centerBtnClicked:(UITapGestureRecognizer *)tap{
NSLog(@"centerBtnPressed");
if ([self.activityDic[@"menu_type"] isEqualToString:@"single"]) {
NSLog(@"showActivity");
//is center button pressed
_isCenterBtnPressed = YES;
self.selectedIndex = 2;
}
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSLog(@"clicked");
//!!!!!!!!!!!!!!!!!!!!!!!!!! 沒有這個方法的話 切換tabbar中間按鈕點擊事件會被tabbar覆蓋
[self.tabBar bringSubviewToFront:self.centerView];
if (self.showCenterBtn&&(viewController == tabBarController.viewControllers[2])) {
NSLog(@"clicked3");
//if is center button pressed ,return yes, select index 2
if (_isCenterBtnPressed) {
//reset to NO
_isCenterBtnPressed = NO;
return YES;
}
return NO;
}
return YES;
}
@end
一個很頭疼的問題,添加過按鈕后第一次點擊中間的按鈕會觸發(fā)按鈕的事件叮喳,但是切換過index后再點擊按鈕后觸發(fā)的就是被按鈕覆蓋了的那個位置的Item的點擊事件被芳,也就是直接切換了控制器。想著應(yīng)該是手勢沖突了馍悟,然后各種百度畔濒,Google,文檔都沒有解決問題最后想了想既然第一次點擊按鈕會觸發(fā)它的點擊事件赋朦,切換過以后點擊事件就被覆蓋了篓冲,和View的層級何其相似,雖然不知道tabbar點擊做了什么處理宠哄,但是這不影響我天馬星空般的思維壹将,沒錯!就是在代理方法中添加了這一句代碼:
[self.tabBar bringSubviewToFront:self.centerView];
然后世界都變成彩色的了……