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;//}
先发一段,后续整理
阅读全文
0 0
- iOS 获得iPhone iPad TabbarItem Frame 深度解析
- iphone, iOS, ipad图标。
- ios is IPhone or IPad
- IOS iPhone、iPad屏幕尺寸
- iphone, ipad, ios的启动图片。
- ios如何取得iphone/ipad的磁盘空间
- iOS iPhone XIB资源批量转换iPad
- [ios]howto - IPAD运行IPHONE程序全屏
- ios 从iphone xib 转变为 ipad
- iOS判断是ipad还是iphone
- ios判断设备是iphone还是ipad
- ios xib适配iPhone,iPad语句
- ios 判断iPhone、iPad硬件型号
- iOS(iPhone,iPad))开发(Obje…
- iOS 判断设备为iPhone还是iPad
- Building OpenSSL for iOS (iPhone/iPad)
- iOS 判断iphone ipad 系统版本
- iOS数据分享(兼容iPad,iphone)
- ThinkPHP3.2.3查询两个日期之间的数据
- 微信小程序 各种页面特效链接
- 不平衡数据分类算法介绍与比较
- DBUtils
- 红黑二叉树详解及理论分析
- iOS 获得iPhone iPad TabbarItem Frame 深度解析
- IOS-忽略的一个小小问题
- 前端HTML5几种存储方式的总结
- AndroidStudio创建Compile依赖
- 什么是表单验证
- Android Studio项目上传到码云问题总结
- HDU4027-Can you answer these queries?(线段树+思路)
- 8大排序算法图文讲解
- eclipse从SVN 引入的项目无法编译