博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 获得iPhone iPad TabbarItem Frame 深度解析
阅读量:4297 次
发布时间:2019-05-27

本文共 3458 字,大约阅读时间需要 11 分钟。

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

先发一段,后续整理

转载地址:http://dkdws.baihongyu.com/

你可能感兴趣的文章
关于PCB设计中过孔能否打在焊盘上的两种观点
查看>>
PCB反推理念
查看>>
京东技术架构(一)构建亿级前端读服务
查看>>
git 提示:error: unable to rewind rpc post data - try increasing http.postBuffer
查看>>
php 解决json_encode中文UNICODE转码问题
查看>>
LNMP 安装 thinkcmf提示404not found
查看>>
PHP empty、isset、innull的区别
查看>>
apache+nginx 实现动静分离
查看>>
通过Navicat远程连接MySQL配置
查看>>
phpstorm开发工具的设置用法
查看>>
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>