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] == ) {
// 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] == ) {
// 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;
}
iOS 获取 UITabViewController 和 UINavigationController 的图标位置的更多相关文章
- iOS 获取文件的目录路径的几种方法 [转]
iOS 获取文件的目录路径的几种方法 2 years ago davidzhang iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. d ...
- iOS获取各种数据方法整理以及IDFA与IDFV使用环境
iOS获取APP版本号: NSString *AppVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBun ...
- iOS 获取当前经纬度
一般说来LBS功能一般分为两块:一块是地理定位,就是获取当前精度.纬度和地理位置的功能,这一部分功能主要用到CoreLocation.Frameworks.一部分就是显示地图信息.丰富地图内容等,这一 ...
- 解决mxGraph放大/缩小在非IE浏览器下overlay图标位置不变化的问题
首先要创建一个工具栏.并为工具栏中的放大.缩小button定义事件. <div id="toolbar" style="float:left;margin-top: ...
- iOS获取设备唯一标识的8种方法
8种iOS获取设备唯一标识的方法,希望对大家有用. UDID UDID(Unique Device Identifier),iOS 设备的唯一识别码,是一个40位十六进制序列(越狱的设备通过某些工具可 ...
- iOS获取设备型号、装置类型等信息
iOS获取设备型号.设备类型等信息 设备标识 关于设备标识,历史上盛行过很多英雄,比如UDID.Mac地址.OpenUDID等,然而他们都陆陆续续倒在了苹果的门下.苹果目前提供了2个方法供App获取设 ...
- Swift3.0 iOS获取当前时间 - 年月日时分秒星期
Swift3.0 iOS获取当前时间 - 年月日时分秒星期func getTimes() -> [Int] { var timers: [Int] = [] // 返回的数组 let calen ...
- IOS 获取最新设备型号方法
1.IOS 获取最新设备型号方法列表最新对照表:http://theiphonewiki.com/wiki/Models方法: #import "sys/utsname.h” struct ...
- ios 获取通讯录的所有信息
iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBoo ...
随机推荐
- AnyConnect removes "Connections" tab from IE Settings solution
I have an ASA 5510 that we use for SSL VPN Client access. The ASA distributes the AnyConnect (v2.4. ...
- javascript常用验证大全
1. 长度限制 <script> function test() { if(document.a.b.value.length>50) { alert("不能超过50个字符 ...
- js练习 closure
window.onload = function() { for (var i = 1; i < 4; i++) { var id = doc ...
- Python3 min() 函数
Python3 min() 函数 Python3 数字 描述 min() 方法返回给定参数的最小值,参数可以为序列. 语法 以下是 min() 方法的语法: min( x, y, z, .... ) ...
- AFNetworking 不支持 text/plain,unacceptable content-type: text/plain
1. 用AFNetworkingPOST传递参数(获取微博的accessToken)的时候,具体代码如下: AFHTTPSessionManager *session = [AFHTTPSession ...
- 网页截图API接口,一键自动生成网页截图
背景 最近在开发一个小程序,其中有一个帮助模块,内容为帮助文章列表,文章内容为网站后台编辑的富文本格式.鉴于小程序的特殊性,其对html格式的富文本支持并不友好. 刚开始有人开发了wxparse插件, ...
- Cplus Overolad new and delete Operator
思考:在C++类中,通过设计类的构造和析构函数,就已经把复杂的内存管理起来了. 及时是简单的结构体,也是有构造和析构函数的,而下面这种情况,可以在非结构中使用. /** Operator Overlo ...
- Oracle ADF VO排序及VO的查询模式
常规应用中,当需要使用Table向终端用户展示数据时,Table中数据的显示排序一致性极大程度的影响到了客户体验.通常希望诸如多次查询结果显示顺序相同.插入数据在原数据上方等的实现. ADF为开发人员 ...
- 五款免费的UML建模工具
1.免费UML建模工具推荐:JUDE–community 2.免费UML建模工具推荐:UMLet 3.免费UML建模工具推荐:ArgoUML 4.免费UML建模工具推荐:BOUml 5.免费UML建模 ...
- 从iOS 11 UI Kit中谈谈iOS 11的新变化
北京时间9月20日凌晨1点,iOS 11终于迎来了正式版的推送,相信各位小伙伴已经在第一时间进行了升级.iOS 11毫无疑问是一次大规模的系统更新,UI.系统内核.锁屏等多方面都进行了不同程度的改进. ...