在窗口中找出UIBarButtonItem框架? [英] Figure out UIBarButtonItem frame in window?

查看:98
本文介绍了在窗口中找出UIBarButtonItem框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UIBarButtonItem 不会扩展 UIView ,因此没有框架属性。

UIBarButtonItem does not extend UIView, so there is nothing like a frame property.

但是有任何方式我可以得到什么是 CGRect 框架,相对于应用程序 UIWindow

But is there any way I can get what is it's CGRect frame, relative to the application UIWindow?

推荐答案

您要使用私人API ?如果是,

UIView* view = thatItem.view;
return [view convertRect:view.bounds toView:nil];






当然,没有人想要这个。一个更不可靠的方法,并且还使用未记录的功能,但将通过Apple的测试,就是循环遍历子视图来寻找相应的按钮项。


Of course no one wants this when targeting the AppStore. A more unreliable method, and also uses undocumented features, but will pass Apple's test, is to loop through the subviews to look for the corresponding button item.

NSMutableArray* buttons = [[NSMutableArray alloc] init];
for (UIControl* btn in theToolbarOrNavbar.subviews)
  if ([btn isKindOfClass:[UIControl class]])
    [buttons addObject:btn];
UIView* view = [buttons objectAtIndex:index];
[buttons release];
return [view convertRect:view.bounds toView:nil];

索引在删除所有空白项目后, .items 数组中的假设按钮按升序排列,这可能不是。更可靠的方法是在增加 .origin.x 值中排序按钮 。当然,这仍然假设条形图按钮项必须继承UIControl类,并且是工具栏/导航栏的直接子视图,也可能不是。

The index is the index to your bar item in the array of .items, after removing all blank items. This assumes the buttons are arranged in increasing order, which may not be. A more reliable method is to sort the buttons array in increasing .origin.x value. Of course this still assumes the bar button item must inherit the UIControl class, and are direct subviews of the toolbar/nav-bar, which again may not be.

正如您所看到的,在处理未记录的功能时有很多不确定性。但是,你只是想弹出一些东西在手指下? UIBarButtonItem的 .action 可以是以下形式的选择器:

As you can see, there are a lot of uncertainty when dealing with undocumented features. However, you just want to pop up something under the finger right? The UIBarButtonItem's .action can be a selector of the form:

-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event;

注意事件参数 - 您可以获得触摸位置

note the event argument — you can obtain the position of touch with

[[event.allTouches anyObject] locationInView:theWindow]

或按钮视图与

[[event.allTouches anyObject] view]

因此,不需要迭代子视图或使用未记录的功能来做什么。

Therefore, there's no need to iterate the subviews or use undocumented features for what you want to do.

这篇关于在窗口中找出UIBarButtonItem框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆