[英]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.