簡單實現UITabBarController,TabBar自定義,使中間按鈕放大

本文通過繼承UITabBarController,簡單實現中間按鈕放大功能

樣式:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface MainTabBarViewController : UITabBarController
+ (instancetype)instance;

@end

#import "MainTabBarViewController.h"
#import "BaseNavigationController.h"
#import "HomeNewViewController.h"

#import "AppDelegate.h"


@interface MainTabBarViewController ()<UITabBarControllerDelegate>



@end

@implementation MainTabBarViewController
+ (instancetype)instance {
    AppDelegate *delegete = (AppDelegate *)[UIApplication sharedApplication].delegate;
    UIViewController *vc = delegete.window.rootViewController;
    if ([vc isKindOfClass:[MainTabBarViewController class]]) {
        return (MainTabBarViewController *)vc;
    }else{
        return nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //    [[UINavigationBar appearance] setTintColor: THEME_GRAY_COLOR];
    //    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_mainGray_color,NSFontAttributeName:TEXT_LIT_B_FONT13} forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName:k_main_bar_color,NSFontAttributeName:TEXT_LIT_B_FONT13} forState:UIControlStateSelected];

    self.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBar.barTintColor = [UIColor whiteColor] ;
    self.tabBar.alpha = 1.0f;
    self.tabBar.tintColor = k_white_color ;
    //解決這個問題是 iOS 12.1 Beta 2 引入的問題,只要 UITabBar 是磨砂的,並且 push viewController 時 hidesBottomBarWhenPushed = YES 則手勢返回的時候就會觸發,出現這個現象的直接原因是 tabBar 內的按鈕 UITabBarButton 被設置了錯誤的 frame,frame.size 變爲 (0, 0) 導致的。
    [UITabBar appearance].translucent = NO;
    

    //首頁    
    HomeNewViewController *homeVC = [[HomeNewViewController alloc]init];
    BaseNavigationController *homeNVC = [[BaseNavigationController alloc] initWithRootViewController:homeVC];
    HomeNewViewController *homeVC2 = [[HomeNewViewController alloc]init];
    BaseNavigationController *homeNVC2 = [[BaseNavigationController alloc] initWithRootViewController:homeVC2];
    HomeNewViewController *homeVC3 = [[HomeNewViewController alloc]init];
    BaseNavigationController *homeNVC3= [[BaseNavigationController alloc] initWithRootViewController:homeVC3];
    HomeNewViewController *homeVC4 = [[HomeNewViewController alloc]init];
    BaseNavigationController *homeNVC4 = [[BaseNavigationController alloc] initWithRootViewController:homeVC4];
    HomeNewViewController *homeVC5 = [[HomeNewViewController alloc]init];
    BaseNavigationController *homeNVC5 = [[BaseNavigationController alloc] initWithRootViewController:homeVC5];
    homeNVC.title = @"1111";
    homeNVC2.title = @"2222";
    homeNVC3.title = @"3333";
    homeNVC4.title = @"4444";
    homeNVC5.title = @"5555";
    self.viewControllers = @[homeNVC, homeNVC2,homeNVC3, homeNVC4, homeNVC5];
    
    homeNVC.tabBarItem = [self generateTabBarItem:@"任務" imgName:@"tabbar_renwu_unselect" selectedImgName:@"tabbar_renwu_select"];
    homeNVC2.tabBarItem = [self generateTabBarItem:@"案例" imgName:@"tabbar_anliku_unselect" selectedImgName:@"tabbar_anliku_select"];
    homeNVC3.tabBarItem = [self generateTabBarItem:@"" imgName:@"tabbar_wodekehu_unselect" selectedImgName:@"tabbar_wodekehu_select"];
    homeNVC4.tabBarItem = [self generateTabBarItem:@"雲課堂" imgName:@"tabbar_yunketang_unselect" selectedImgName:@"tabbar_yunketang_select"];
    homeNVC5.tabBarItem = [self generateTabBarItem:@"我的" imgName:@"tabbar_wode_unselect" selectedImgName:@"tabbar_wode_select"];
    
}

//實現中間按鈕放大的關鍵代碼
-(void)viewWillLayoutSubviews{
    
    for (int i = 0; i<self.tabBar.items.count; i ++) {
        UITabBarItem *itm = self.tabBar.items[i];
        if (i==2) {
             itm.imageInsets = UIEdgeInsetsMake(-10, -10, -10, -10);
         }
    }
}

-(UITabBarItem*)generateTabBarItem:(NSString*)title imgName:(NSString*)imgName selectedImgName:(NSString*)selectedImgName{

    UIImage *img = [[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selectedImg = [[UIImage imageNamed:selectedImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    return [[UITabBarItem alloc] initWithTitle:title image:img selectedImage:selectedImg];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

通過設置 UITabBarItem的 imageInsets 來實現的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章