iOS中使用者未登入狀態下點選下方tabBar觸發登入
前提 公司沒有設計訪客檢視介面
效果圖片
接下來直接上程式碼
@interface CustomTabBarController : UITabBarController<UITabBarDelegate,UITabBarControllerDelegate>
self.delegate = self;
//重點在這裡#pragma mark - 新增tabbaritem
- (void)addTabBarItems{
UITabBar *bar = self.tabBar;
UITabBarItem *homeItem = bar.items
[selfsetTabBarItem:homeItem image:@"sy_013"selectedImage:@"sy_017"title:@"摩點"tag:1];
UITabBarItem *dynamicItem1 = bar.items[1];
[selfsetTabBarItem:dynamicItem1 image:@"MD"selectedImage:@"MD2"title:@"專案"tag:2];
UITabBarItem *dynamicItem = bar.items[2];
[selfsetTabBarItem:dynamicItem
UITabBarItem *inviteFriendsItem = bar.items[3];
[selfsetTabBarItem:inviteFriendsItem image:@"sy_015"selectedImage:@"sy_019"title:@"朋友"tag:4];
UITabBarItem *personalCenterItem = bar.items[4];
[selfsetTabBarItem:personalCenterItem
}
- (void)setTabBarItem:(UITabBarItem *)item image:(NSString *)image selectedImage:(NSString *)selected title:(NSString *)title tag:(NSInteger)tag{
item.image = [[UIImageimageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.selectedImage = [[UIImageimageNamed:selected] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.title = title;
item.tag = tag;
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorblackColor],NSFontAttributeName:[UIFontsystemFontOfSize:12]}forState:UIControlStateNormal];
[item setTitleTextAttributes:@{NSForegroundColorAttributeName: kColor(22, 180, 96)}forState:UIControlStateSelected];
}
#pragma mark - UITabBarControllerDelegate
//這個方法根據官方文件解釋意思就是點選下面的tabBar的按鈕時候 根據BOOL值來判斷是否處於可繼續點選狀態
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0){
if (viewController.tabBarItem.tag == 3 || viewController.tabBarItem.tag == 4){
if (![Context logined]) {
CKLoginVC *viewFlag = [[CKLoginVC alloc] init];
UINavigationController *navi = [[UINavigationControlleralloc] initWithRootViewController:viewFlag];
[selfpresentViewController:navi animated:YEScompletion:^{
}];
return NO;
}else
{
return YES;
}
} else
{
return YES;
}
}