[英]How do I get the width of a UITabBarItem?
UITabBarItem的宽度会有所不同,具体取决于有多少。
如何确定标签栏项目的宽度?
如果可能的话,我正在寻找一个属性,而不是数学公式,因为在iPad上,标签栏的两侧也存在填充问题。 考虑这些截图。 注意iPad上标签栏项目两侧的填充(用红色框突出显示)。 iPhone上不存在此填充。
iPad:
iPhone:
编辑 :在下面的评论中已经注意到此解决方案在iOS 5的测试版中不起作用,因此请准备好修改它以满足您的需求。
我认为为了以正确的方式做到这一点,你必须能够到达每个标签栏项目的框架。 幸运的是,这是可能的:
CGFloat tabBarTop = [[[self tabBarController] tabBar] frame].origin.y;
NSInteger index = [[self tabBarController] selectedIndex];
CGFloat tabMiddle = CGRectGetMidX([[[[[self tabBarController] tabBar] subviews] objectAtIndex:index] frame]);
上面的代码为您提供了选项卡栏顶部的y坐标和所选选项卡项中间的x坐标。 从这里,您可以将指标视图添加到相对于此点的选项卡栏控制器视图中。
免责声明 :这种方法的危险在于它通过访问其视图层次结构和私有UITabBarButton
类的实例的frame
属性来窥视UITabBar
类的UITabBarButton
。 未来的iOS更新可能会(但不太可能)影响/破坏此方法。 但是,您没有访问任何私有API,因此不应该从App Store中拒绝您的应用程序。
我制作了一个简单的iPad项目,展示了它的工作原理 ,并完成了动画制作。
斯威夫特3
我在UIApplication中创建了一个共享实例,然后拉出了我的子视图宽度
tabBarController?.tabBar.subviews[1].frame.width
我尝试了UITabBar的这两个属性:
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
但Vive说,获得零价值。 所以我写了一些代码来获得正确的宽度:
CGFloat tabBarItemWidth = 0;
for (UIView *view in [self.tabBarController.tabBar subviews]) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
if (tabBarItemWidth == 0) {
tabBarItemWidth = view.frame.origin.x;
} else {
tabBarItemWidth = view.frame.origin.x - tabBarItemWidth;
break;
}
}
}
为什么不使用第一个UITabBarButton的宽度? 因为标签栏按钮之间有空格。 :)
看看上面的答案:
UITabBar
项目在iPhone和iPad中的工作方式不同。 在iPhone中,它们填充整个宽度,在iPad中它们是水平居中的。 tabBar.itemWidth
或tabBar.itemSpacing
设置宽度和间距的值。 您无法从中读取系统间距或宽度 - 除非您设置它,否则您将收到0
。 所以这是我的示例如何获取所有UITabBarItems
帧:
// get all UITabBarButton views
NSMutableArray *tabViews = [NSMutableArray new];
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:[UIControl class]]) {
[tabViews addObject:[NSValue valueWithCGRect:view.frame]];
}
}
// sort them from left to right
NSArray *sortedArray = [tabViews sortedArrayUsingComparator:^NSComparisonResult(NSValue *firstValue, NSValue *secondValue) {
CGRect firstRect = [firstValue CGRectValue];
CGRect secondRect = [secondValue CGRectValue];
return CGRectGetMinX(firstRect) > CGRectGetMinX(secondRect);
}];
现在您有一个框架表,对应于您的tabbar项目。 你可以,例如。 在所选索引按钮框架上放置一个imageView。
CGRect frame = self.tabBar.bounds;
CGSize imageSize = CGSizeMake(CGRectGetWidth(frame) / self.tabBar.items.count, self.imageView.image.size.height);
CGRect selectedRect = sortedArray.count > self.selectedIndex ? [sortedArray[self.selectedIndex] CGRectValue] : CGRectZero;
[self.imageView setFrame:CGRectIntegral(CGRectMake(CGRectGetMinX(selectedRect), CGRectGetMaxY(frame) - imageSize.height,
CGRectGetWidth(selectedRect), imageSize.height))];
您可以使用私有属性view
获取tabBarItem view
。 然后只是获取视图frame
。
- (CGRect)rectForTabBarItem:(UITabBarItem *)tabBarItem {
UIView *itemView = [tabBarItem valueForKey:@"view"];
return itemView.frame;
}
UITabBarItem继承自UIBarItem,它继承自NSObject。 由于它不是从UIView继承,我不知道你将能够获得你想要的信息只是一个属性。 我知道你不需要一种数学方法来做这件事,但似乎获得标签栏的框架并除以标签数量将是一个合理的选择,无论硬件如何都应该有效(iphone vs ipad) 。 填充不应该影响这个,对吧? 所以你有:
tabSize = tabbar.frame.size.width / [tabbar.items count];
tabBarStart = tabbar.frame.origin.x;
// Assume index of tabs starts at 0, then the tab in your pic would be tab 4
// targetX would be the center point of the target tab.
targetX = tabBarStart + (tabSize * targetTabIndex) + (tabSize / 2);
我也有兴趣知道是否有人找到了一个简单的属性来提供这些信息。
循环浏览tabBar的子视图,并使用“UITabBarButton”类查找视图。 这些是每个标签项的视图。
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
// view.frame contains the frame for each item's view
// view.center contains the center of each item's view
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.