码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 获取 UITabViewController 和 UINavigationController 的图标位置

时间:2015-12-09 19:12:41      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:

这些图标是放在 UITabBar 和 UINavigationBar 里的。所以只要遍历它们的 subViews,找到类型是 UIButton 的就可以了。

所有想获取它们的相对位置很容易。

 

但我有个需求是需要在整个屏幕覆盖一层 UIView,然后在 这些图标旁画一些图案(其实我是在做应用教程)。这时候就需要获取他们在整个视图中相对于最外层的 UIViewController 的位置。这就没有那么容易了。

我的应用中,最外层有一个UITabBarController,里面嵌套一个 UINavigationController,里面在嵌套一个 UIViewController。由于 UITabBarController 位于最外层,所以获取到的位置就是想要的位置。而 UINavigationBar 里的图标就需要做些调整。也就是需要加入 UITabBarController 的 layout margin。经过测试,一般尺寸的 iPhone 这个值为16,而 iPhone 6plus,iPad 这个值20。

所以我想要的位置就是:

  1. 左侧的图标

    获取的相对位置+layoutMargin.left

  2. 右侧的图标 

    获取的相对位置-layoutMargin.right

 

获取相对位置的代码:

+ (CGPoint)centerForTabInTabBar:(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 CGPointZero;
    }
    
    // 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;
    }];
    
    CGPoint center = CGPointZero;
    if (index < [tabBarItems count]) {
        // viewController is in a regular tab
        UIView *tabView = tabBarItems[index];
        center = tabView.center;
    }
    else {
        // our target viewController is inside the "more" tab
        UIView *tabView = [tabBarItems lastObject];
        center = tabView.center;
    }
    return center;
}

 

+ (CGPoint)centerForItemInNavigationBar:(UINavigationBar *)navigationBar withIndex:(NSUInteger)index
{
    NSMutableArray *navigationBarItems = [NSMutableArray arrayWithCapacity:[navigationBar.items count]];
    for (UIView *view in navigationBar.subviews) {
        if ([view isKindOfClass:NSClassFromString(@"UIButton")] && [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
            [navigationBarItems addObject:view];
        }
    }
    if ([navigationBarItems 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 CGPointZero;
    }
    
    // sort by origin.x of the frame because the items are not necessarily in the correct order
    [navigationBarItems 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;
    }];
    
    CGPoint point = CGPointZero;
    if (index < [navigationBarItems count]) {
        // viewController is in a regular tab
        UIView *navigationView = navigationBarItems[index];
        if ([navigationView respondsToSelector:@selector(frame)]) {
            point = navigationView.center;
        }
    }
    else {
        // our target viewController is inside the "more" tab
        UIView *navigationView = [navigationBarItems lastObject];
        if ([navigationView respondsToSelector:@selector(frame)]) {
            point = navigationView.center;
        }
    }
    return point;
}

 

iOS 获取 UITabViewController 和 UINavigationController 的图标位置

标签:

原文地址:http://www.cnblogs.com/davesuen/p/5033468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!