iOS 获得iPhone iPad TabbarItem Frame 深度解析

来源:互联网 发布:电脑桌面软件怎么变小 编辑:程序博客网 时间:2024/06/16 14:39
- (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 = <CALayer: 0x174026b40>>,<UITabBarButton: 0x101a9f5e0; frame = (77 1; 71 48); opaque = NO; layer = <CALayer: 0x174236140>>,<UITabBarButton: 0x101aa2f10; frame = (152 1; 71 48); opaque = NO; layer = <CALayer: 0x1742365c0>>,<UITabBarButton: 0x101c8a3e0; frame = (227 1; 71 48); opaque = NO; layer = <CALayer: 0x17042fac0>>,<UITabBarButton: 0x101aa77c0; frame = (302 1; 71 48); opaque = NO; layer = <CALayer: 0x17042eea0>>,<UITabBarButton: 0x101abfcd0; frame = (2 1; 71 48); opaque = NO; layer = <CALayer: 0x170436dc0>>)

+ (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;//}


先发一段,后续整理




原创粉丝点击