获取特定标签栏项的框架 [英] Getting the frame of a particular tab bar item

查看:88
本文介绍了获取特定标签栏项的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 UITabBar 中找到特定 UITabBarItem 的框架?

Is there a way to find the frame of a particular UITabBarItem in a UITabBar?

具体来说,我想创建一个图像下降到其中一个标签的动画,类似于例如删除邮件中的电子邮件,或在iTunes应用程序中购买曲目。所以我需要动画的目标坐标。

Specifically, I want to create an animation of an image "falling" into one of the tabs, similar to e.g. deleting an email in the Mail, or buying a track in the iTunes app. So I need the target coordinates for the animation.

据我所知,没有公共API可以获得坐标,但是我不想错。除此之外,我将不得不使用相对于标签栏框架的给定标签的索引来猜测坐标。

As far as I can tell, there's no public API to get the coordinates, but would love to be wrong about that. Short of that, I'll have to guesstimate the coordinates using the index of the given tab relative to the tab bar frame.

推荐答案

Imre的实施缺少一些重要的细节。

Imre's implementation is missing a couple of imho important details.


  1. UITabBarButton视图不一定是有序的。例如,如果iPhone上有超过5个选项卡并重新排列选项卡,则视图可能会出现故障。

  2. 如果使用超过5个选项卡,则越界索引仅表示该选项卡位于更多选项卡后面。在这种情况下,没有理由使用断言失败,只需使用最后一个选项卡的框架。

所以我改变了他的代码,我想出了这个:

So I changed his code a little bit and I came up with this:

+ (CGRect)frameForTabInTabBar:(UITabBar*)tabBar withIndex:(NSUInteger)index
{
    NSMutableArray *tabBarItems = [NSMutableArray arrayWithCapacity:[tabBar.items count]];
    for (UIView *view in tabBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")] && [view respondsToSelector:@selector(frame)]) {
            // check for the selector -frame to prevent crashes in the very unlikely case that in the future
            // objects thar don't implement -frame can be subViews of an UIView
            [tabBarItems addObject:view];
        }
    }
    if ([tabBarItems count] == 0) {
        // no tabBarItems means either no UITabBarButtons were in the subView, or none responded to -frame
        // return CGRectZero to indicate that we couldn't figure out the frame
        return CGRectZero;
    }

    // sort by origin.x of the frame because the items are not necessarily in the correct order
    [tabBarItems sortUsingComparator:^NSComparisonResult(UIView *view1, UIView *view2) {
        if (view1.frame.origin.x < view2.frame.origin.x) {
            return NSOrderedAscending;
        }
        if (view1.frame.origin.x > view2.frame.origin.x) {
            return NSOrderedDescending;
        }
        NSAssert(NO, @"%@ and %@ share the same origin.x. This should never happen and indicates a substantial change in the framework that renders this method useless.", view1, view2);
        return NSOrderedSame;
    }];

    CGRect frame = CGRectZero;
    if (index < [tabBarItems count]) {
        // viewController is in a regular tab
        UIView *tabView = tabBarItems[index];
        if ([tabView respondsToSelector:@selector(frame)]) {
            frame = tabView.frame;
        }
    }
    else {
        // our target viewController is inside the "more" tab
        UIView *tabView = [tabBarItems lastObject];
        if ([tabView respondsToSelector:@selector(frame)]) {
            frame = tabView.frame;
        }
    }
    return frame;
}

这篇关于获取特定标签栏项的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆