iOS 获得iPhone iPad TabbarItem Frame 深度解析

- (void)viewDidLoad {
    [super viewDidLoad];
    //iPhone上最多显示5个tabbarItem ipad上最多显示8个tabbarItem 高度固定为48
    //iPhone 上item不论几个(不少以2个)总是均匀分布的 item的实际宽度(因部分屏幕宽度除不尽count,的除外),(WIDTH - items.count * 2 * 2) / items.count
    //iPad上 item为8个时,是均匀分布,少于8个时,以屏幕为中心,Item宽度固定76,Item间距固定34,向屏幕两边延展。
   CGRect itemFrame = [self getTabBarItemFrameWithCount:self.tabBarController.tabBar.items.count index:2];
    
//    NSLog(@"itemFrame x: %f y: %f w: %f h: %f",itemFrame.origin.x,itemFrame.origin.y,itemFrame.size.width,itemFrame.size.height);
//    NSLog(@"均分 WIDTH %f,",WIDTH);
//    NSLog(@"均分 WIDTH %@,",self.tabBarController.tabBar);
//    NSLog(@"均分 all %f,",itemFrame.size.width * self.tabBarController.tabBar.items.count);
//    if (fabsf((itemFrame.size.width - ((WIDTH - self.tabBarController.tabBar.items.count * 2 * 2) / self.tabBarController.tabBar.items.count))) <= 1) {
//        NSLog(@"均分");
//    }
    UIView *view = [[UIView alloc] initWithFrame:itemFrame];
    view.backgroundColor = [UIColor purpleColor];
    view.alpha = 0.4;
    [self.tabBarController.tabBar addSubview:view];
    //测试弹窗
//    [self alertControllerTest];
}

#pragma mark-
#pragma mark- 获得tabbarItem的frame
//return CGRectZero 则获取失败
- (CGRect)getTabBarItemFrameWithCount:(NSInteger)count index:(NSInteger)index
{
    NSInteger i = 0;
    CGRect itemFrame = CGRectZero;
    for (UIView *view in self.tabBarController.tabBar.subviews) {
        if (![NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
            continue;
        }
        //找到指定的tabBarItem
        if (index == i++) {
            itemFrame = view.frame;
            break;
        }
    }
    
    return itemFrame;
}


这个方法竟然遇到了奇葩问题,打印tabbar的subviews

<__NSArrayM 0x17465e060>(
<_UIBarBackground: 0x101c128e0; frame = (0 0; 375 49); userInteractionEnabled = NO; layer = >,
>,
>,
>,
>,
>
)

+ (CGRect)getTabBarItemFrameWithTabBar:(UITabBar *)tabBar index:(NSInteger)index
{
    //遍历出tabBarItems
    NSMutableArray *tabBarItems = [NSMutableArray array];
    for (UIView *view in tabBar.subviews) {
        if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"]) {
            [tabBarItems addObject:view];
        }
    }
    //根据frame的X从小到大对tabBarItems进行排序
    NSArray *sortedTabBarItems= [tabBarItems sortedArrayUsingComparator:^NSComparisonResult(UIView *view1, UIView *view2){
        return [@(view1.frame.origin.x) compare:@(view2.frame.origin.x)];
    }];
    //找到指定的tabBarItem 并优化其相对于屏幕的位置
    NSInteger i = 0;
    CGRect itemFrame = CGRectZero;
    for (UIView *view in sortedTabBarItems) {
        if (index == i) {
            itemFrame = view.frame;
            itemFrame.origin.y = ScreenHeight - itemFrame.size.height;
            break;
        }
        i++;
    }
    
    return itemFrame;
}

//+ (CGRect)getTabBarItemFrameWithTabBar:(UITabBar *)tabBar
//                                 index:(NSInteger)index
//{
//    NSLog(@"tabBar.itemSpacing   %f  %f",tabBar.itemWidth,tabBar.itemSpacing);
//    CGFloat tabBarShowWidth = tabBar.items.count * tabBar.itemWidth + (tabBar.items.count - 1) * tabBar.itemSpacing;
//    CGFloat tabBarShowX = (tabBar.frame.size.width - tabBarShowWidth) * 0.5;
//    CGFloat itemX = tabBarShowX + (index - 1) * (tabBar.itemWidth + tabBar.itemSpacing);
//    CGRect itemFrame = CGRectMake(itemX, ScreenHeight - tabBar.frame.size.height, tabBar.itemWidth, tabBar.frame.size.height);
//
//    return itemFrame;
//}


先发一段,后续整理




你可能感兴趣的:(iOS开发总结,学习笔记)