iOS 获取 UITabViewController 和 UINavigationController 的图标位置
这些图标是放在 UITabBar 和 UINavigationBar 里的。所以只要遍历它们的 subViews,找到类型是 UIButton 的就可以了。
所有想获取它们的相对位置很容易。
获取到相对位置之后,根据容器位置进行调整就可以了。
所以 UITabBar 里图标的位置:
CGPoint center = CGPointMake(centerInTab.x + tabBar.frame.origin.x, centerInTab.y + tabBar.frame.origin.y);
UINavigationBar 里图标的位置:
CGPoint center = CGPointMake(point.x + self.navigationController.navigationBar.frame.origin.x, point.y + self.navigationController.navigationBar.frame.origin.y);
注意:获取位置的这些函数的调用要在 viewDidAppear 之后,否则结果不准确。
获取相对位置的代码:
+ (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 { NSLog(@"centerForItemInNavigationBar: withIndex:%d",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 navigationBarItems means either no UIButton 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; UIView *navigationView = navigationBarItems[index]; NSLog(@"center %f,%f", navigationView.center.x, navigationView.center.y); return navigationView.center; }
移动开发者