首页 文章

如何获得UITabBarItem的宽度?

提问于
浏览
15

UITabBarItem的宽度会有所不同,具体取决于有多少 .

How do I determine how wide my tab bar items are?

如果可能的话,我正在寻找一个属性,而不是数学公式,因为在iPad上,标签栏的两侧也存在填充问题 . 考虑这些截图 . 注意iPad上标签栏项目两侧的填充(用红色框突出显示) . iPhone上不存在此填充 .

The iPad:

enter image description here

iPhone:
enter image description here

7 回答

  • -1

    Edit :在下面的评论中已经注意到此解决方案在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坐标 . 从这里,您可以将指标视图添加到相对于此点的选项卡栏控制器视图中 .

    Disclaimer :这种方法的危险在于它通过访问其视图层次结构和私有 UITabBarButton 类实例的 frame 属性来窥视 UITabBar 类的引擎 . 未来的iOS更新可能会(但不太可能)影响/破坏此方法 . 但是,您将从App Store拒绝您的应用程序 .

    我制作了a simple iPad project demonstrating how it works,完成了动画 .

  • 0

    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);
    

    我也有兴趣知道是否有人找到了一个简单的属性来提供这些信息 .

  • 0

    看看上面的答案:

    • UITabBar 中的项目在iPhone和iPad中的工作方式不同 . 在iPhone中,它们填充整个宽度,在iPad中它们是水平居中的 .

    • iPad中的项目之间有间距 .

    • 您可以使用 tabBar.itemWidthtabBar.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))];
    
  • 0

    我尝试了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说,获得零 Value . 所以我写了一些代码来获得正确的宽度:

    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的宽度?因为标签栏按钮之间有空格 . :)

  • 7

    您可以使用私有属性 view 获取tabBarItem视图 . 然后只是查看 frame .

    - (CGRect)rectForTabBarItem:(UITabBarItem *)tabBarItem {
        UIView *itemView = [tabBarItem valueForKey:@"view"];
        return itemView.frame;
    }
    
  • 0

    斯威夫特3

    我在UIApplication中创建了一个共享实例,然后拉出了我的子视图宽度

    tabBarController?.tabBar.subviews[1].frame.width

  • 0

    循环浏览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
        }
    }
    

相关问题