iOS 获取 UITabViewController 和 UINavigationController 的图标位置
2015-12-09 17:11
507 查看
这些图标是放在 UITabBar 和 UINavigationBar 里的。所以只要遍历它们的 subViews,找到类型是 UIButton 的就可以了。
所有想获取它们的相对位置很容易。
获取到相对位置之后,根据容器位置进行调整就可以了。
所以 UITabBar 里图标的位置:
UINavigationBar 里图标的位置:
注意:获取位置的这些函数的调用要在 viewDidAppear 之后,否则结果不准确。
获取相对位置的代码:
所有想获取它们的相对位置很容易。
获取到相对位置之后,根据容器位置进行调整就可以了。
所以 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; }
相关文章推荐
- 提升UITableView性能-复杂页面的优化
- zzulioj 1818: squee_spoon and his Cube VI 【dp】
- Android开发之浅仿QQ聊天UI和键盘控制
- 'Building workspace' has encountered a problem.
- Java GUI 小程序 任意圆之间的带箭头的连线并可以存储
- hdu 5312 Sequence(数学推导——三角形数)
- iOS 扩展 UINavigationController 出栈返回到先前标记的位置
- VS2015 build kmdf for Window 8.1
- Arduino - DHT11温湿度传感器
- EasyUi我引入的不对吗?为什么没有任何反应?
- Android下uid与多用户释疑(一)
- marquee 轮播滚动吧span、img、div!!
- IOS-开发日记24 - UITableViewCell点击两次才跳转解决办法
- android Installation failed due to invalid URI! 错误处理
- Android之UID and PID
- 点击Cell的时候 出现一个 小对勾的( 确定UITableViewCell AccessoryCheckmark唯一性)
- iOS 从各种效果图颜色标注生成 UIColor
- IntelliJ IDEA的jsp中request等对象无法被解析的解决办法
- squee_spoon and his Cube VI---郑大校赛(求最长子串)
- easyui ValidateBox validType字段设置多个校验规则