美文网首页
关于自定UITabBar适配iPhone X

关于自定UITabBar适配iPhone X

作者: 今年27 | 来源:发表于2018-12-03 16:14 被阅读19次
    1543824526375.jpg

    要做一个类似如上的效果
    很显然要自定义一个TabBarController来继承UITabBarController, 自定义一个TabBar来继承UITabBar
    其核心明显就是要对Tabbar的LayoutSubView方法重新编排控件布局

    #import <UIKit/UIKit.h>
    
    @interface KBTabbarController : UITabBarController
    
    @end
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    NSArray* frameIdArray = [[JSPhotoFrameHandle sharePhotoFrameHandle] readLocalPhotosFrameIDArray];
        SSLinkListViewController* linkList = [[SSLinkListViewController alloc] initWithNibName:NSStringFromClass([SSLinkListViewController class]) bundle:nil];
        [self addChildController:linkList title:NSLocalizedString(@"frame", nil) imageName:@"icon_frame_nor" selectedImageName:@"icon_frame_selected" navVc:[UINavigationController class]];
       
    
        
        LKRecoViewController *recoVC = [[LKRecoViewController alloc] init];
        [self addChildController:recoVC title:NSLocalizedString(@"Recommend", nil) imageName:@"icon_reco_nor" selectedImageName:@"icon_reco_selected" navVc:[UINavigationController class]];
        
    //    ThreeViewController *MoreVc = [[ThreeViewController alloc] init];
    //    [self addChildController:MoreVc title:@"更多" imageName:@"tab4-more" selectedImageName:@"tab4-moreshow" navVc:[UINavigationController class]];
    //    
    //    FourViewController *svc = [[FourViewController alloc] init];
    //    [self addChildController:svc title:@"设置" imageName:@"tab5-file" selectedImageName:@"tab5-fileshow" navVc:[UINavigationController class]];
        
        
        [[UITabBar appearance] setBackgroundImage:[self imageWithColor:[UIColor whiteColor]]];
        //  设置tabbar
        [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
        // 设置自定义的tabbar
        [self setCustomtabbar];
        
        
        
    }
    
    - (void)setCustomtabbar{
    
        KBTabbar *tabbar = [[KBTabbar alloc]init];
        
        [self setValue:tabbar forKeyPath:@"tabBar"];
    
        [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    }
    
    
    
    - (void)centerBtnClick:(UIButton *)btn{
    
        
        NSLog(@"点击了中间");
        UINavigationController* navi = self.viewControllers.firstObject;
        
        NSArray* idArray = [[JSPhotoFrameHandle sharePhotoFrameHandle] readLocalPhotosFrameIDArray];
        if (idArray.count > 0) {
            JSPhotoViewController* photoVC = [[JSPhotoViewController alloc] init];
            photoVC.hidesBottomBarWhenPushed = YES;
            [navi pushViewController:photoVC animated:YES];
        }else{
            if (self.selectedIndex == 0) {
                UINavigationController* navi = self.viewControllers.firstObject;
                SSLinkListViewController* linkList = navi.viewControllers.firstObject;
                [linkList showWindowView];
            }else{
                self.selectedIndex = 0;
            }
    
            
    //        SSGoToBindViewController* vc = [[SSGoToBindViewController alloc] initWithNibName:NSStringFromClass([SSGoToBindViewController class]) bundle:nil];
    //        vc.view.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    //        [self addChildViewController:vc];
    //        [self.view.window addSubview:vc.view];
    //        [vc didMoveToParentViewController:self];
    //        [vc hiddNavBar];
            
        }
    
        
    
        
    
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)addChildController:(UIViewController*)childController title:(NSString*)title imageName:(NSString*)imageName selectedImageName:(NSString*)selectedImageName navVc:(Class)navVc
    {
        
        childController.title = title;
        childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        // 设置一下选中tabbar文字颜色
        
        [childController.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor darkGrayColor] }forState:UIControlStateSelected];
        
        UINavigationController* nav = [[navVc alloc] initWithRootViewController:childController];
        nav.navigationBarHidden = YES;
        [self addChildViewController:nav];
    }
    
    
    - (UIImage *)imageWithColor:(UIColor *)color{
        // 一个像素
        CGRect rect = CGRectMake(0, 0, 1, 1);
        // 开启上下文
        UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
        [color setFill];
        UIRectFill(rect);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return image;
    }
    
    

    自定义一个TabBar

    #import <UIKit/UIKit.h>
    
    @interface KBTabbar : UITabBar
    
    @property (nonatomic, strong) UIButton *centerBtn;
    
    @end
    
    #import "KBTabbar.h"
    
    @implementation KBTabbar
    
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            
            
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setImage:[UIImage imageNamed:@"icon_add"] forState:UIControlStateNormal];
            btn.bounds = CGRectMake(0, 0, 64, 44);
            self.centerBtn = btn;
            [self addSubview:btn];
            
     
            
        }
        return self;
    }
    
    - (void)layoutSubviews
    {
        
        [super layoutSubviews];
        
        self.centerBtn.frame = CGRectMake(self.bounds.size.width/2 - self.centerBtn.frame.size.width/2, 0, self.centerBtn.frame.size.width, self.centerBtn.frame.size.height);
        
        int index = 0;
        CGFloat wigth = (self.bounds.size.width - self.centerBtn.bounds.size.width)/ 2;
        
        for (UIView* sub in self.subviews) {
            
            if ([sub isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                
               
                NSLog(@"-->%@ --->%d", sub, index);
                
                if (index == 0) {
                    sub.frame = CGRectMake(index * wigth, self.bounds.origin.y, wigth, sub.frame.size.height);
                }else if (index == 1) {
                    sub.frame = CGRectMake(index * wigth + self.centerBtn.bounds.size.width, self.bounds.origin.y, wigth,  sub.frame.size.height);
                }
                    index++;
                
            }
            
        }
    }
    
    
    
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        
        if (self.isHidden == NO) {
            
            CGPoint newPoint = [self convertPoint:point toView:self.centerBtn];
            
            if ( [self.centerBtn pointInside:newPoint withEvent:event]) {
                return self.centerBtn;
            }else{
                
                return [super hitTest:point withEvent:event];
            }
        }
        
        else {
            return [super hitTest:point withEvent:event];
        }
    }
    

    上述适配iPhone X, 我觉得如上的方法已经足够了, 当然如果你还想tabbar往下面移动一点点, 可以在tabbar的viewWillLayoutSubviews中修改

     if (isIphoneX) {
    
            CGRect frame = self.tabBar.frame;
            frame.size.height = 49;
            frame.origin.y = self.view.frame.size.height - frame.size.height;
            self.tabBar.frame = frame;
            for (UITabBarItem *item in self.tabBar.items) {
                item.imageInsets = UIEdgeInsetsMake(15,0, -15, 0);
                [item setTitlePositionAdjustment:UIOffsetMake(0, 32)];
            }
        }
    

    相关文章

      网友评论

          本文标题:关于自定UITabBar适配iPhone X

          本文链接:https://www.haomeiwen.com/subject/kngycqtx.html