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

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

问题描述

UIBarButtonItem 没有扩展 UIView,所以没有像框架属性那样的东西.

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

但是有什么方法可以让我得到相对于应用程序 UIWindowCGRect 框架?

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

推荐答案

你喜欢使用 私有 API?如果是,

Do you like to use private APIs? If yes,

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

<小时>

当然,当以 AppStore 为目标时,没有人想要这个.一个更不可靠的方法,也使用未记录的功能,但会通过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];

index.items 数组中您的栏项的索引,删除所有空白项后.这假设按钮按递增顺序排列,但可能不是.一种更可靠的方法是在增加 .origin.x 值的情况下对 buttons 数组进行排序.当然,这仍然假设栏按钮项必须继承 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;

注意event参数——你可以用

[[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天全站免登陆