传送门:http://stackoverflow.com/questions/6325457/getting-the-frame-of-a-particular-tab-bar-item
我稍微改了一下:
- (CGRect)frameForTabInTabBarWithIndex:(NSUInteger)index { NSMutableArray *tabBarItems = [NSMutableArray arrayWithCapacity:[self.tabBar.items count]]; for (UIView *view in self.tabBar.subviews) { if ([view isKindOfClass:[UIControl class]] && [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 __block BOOL sthWrong = NO; [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; } /// should't be here sthWrong = YES; return NSOrderedAscending; }]; if (sthWrong) { return CGRectZero; } 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; } } return frame; }
有了这些frame后,就可以来实现打红点之类的事情了~