Created
August 14, 2015 09:37
-
-
Save albertodebortoli/e96204dca9732419b0f0 to your computer and use it in GitHub Desktop.
Get the tabbar item view.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UIView *)viewForTabInTabBar:(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 nil; | |
} | |
// 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; | |
} | |
return NSOrderedSame; | |
}]; | |
UIView *retVal = nil; | |
if (index < [tabBarItems count]) { | |
// viewController is in a regular tab | |
UIView *tabView = tabBarItems[index]; | |
if ([tabView respondsToSelector:@selector(frame)]) { | |
retVal = tabView; | |
} | |
} | |
else { | |
// our target viewController is inside the "more" tab | |
UIView *tabView = [tabBarItems lastObject]; | |
if ([tabView respondsToSelector:@selector(frame)]) { | |
retVal = tabView; | |
} | |
} | |
return retVal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment