找出窗口中的 UIBarButtonItem 框架?
UIBarButtonItem
不扩展 UIView
,因此没有像框架属性那样的东西。
但是有什么方法可以获取相对于应用程序 UIWindow
的 CGRect
框架吗?
UIBarButtonItem
does not extend UIView
, so there is nothing like a frame property.
But is there any way I can get what is it's CGRect
frame, relative to the application UIWindow
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您喜欢使用私有 API 吗?如果是的话,
当然没有人希望在针对 AppStore 时出现这种情况。一种更不可靠的方法,也使用了未记录的功能,但会通过Apple的测试,是循环遍历子视图来查找相应的按钮项。
index
是.items
数组中栏项目的索引,删除所有空白项目后。这假设按钮按升序排列,但事实可能并非如此。更可靠的方法是以递增的.origin.x
值对buttons
数组进行排序。当然,这仍然假设栏按钮项必须继承 UIControl 类,并且是工具栏/导航栏的直接子视图,但也可能不是。正如您所看到的,在处理未记录的功能时存在很多不确定性。但是,您只想在手指下弹出一些东西,对吧? UIBarButtonItem 的
.action
可以是以下形式的选择器:注意 event 参数 - 您可以获取触摸的位置
或按钮视图,
因此,无需迭代子视图或使用未记录的功能来完成您想要做的事情。
Do you like to use private APIs? If yes,
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.
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 thebuttons
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.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:note the event argument — you can obtain the position of touch with
or the button view with
Therefore, there's no need to iterate the subviews or use undocumented features for what you want to do.
我没有看到这个选项发布(我认为这要简单得多),所以这里是:
I didn't see this option posted (which in my opinion is much simpler), so here it is:
在 iOS 3.2 中,有一种更简单的方法可以通过工具栏按钮显示操作表弹出窗口。只需做这样的事情:
In iOS 3.2, there's a much easier way to show an Action Sheet popover from a toolbar button. Merely do something like this:
这是我用于 WEPopover 项目的实现:(https://github.com/werner77/WEPopover ):
This is the implementation I use for my WEPopover project: (https://github.com/werner77/WEPopover):
只要
UIBarButtonItem
(和UITabBarItem
)不继承自UIView
— 由于历史原因,UIBarItem
继承自NSObject
——这种疯狂还在继续(截至撰写本文时,iOS 8.2 还在继续……)这个线程中的最佳答案显然是 @KennyTM 的。不要傻了,使用私有 API 来查找视图。
这是一个获取
origin.x
排序数组的单行 Swift 解决方案(如 Kenny 的回答 建议的那样) :该数组现在使用
UIBarButtonItem
框架进行origin.x
排序。(如果您觉得有必要阅读更多有关其他人与 UIBarButtonItem 的斗争的信息,我推荐 Ash Furrow 2012 年的博客文章: 探索UIBarButtonItem)
As long as
UIBarButtonItem
(andUITabBarItem
) does not inherit fromUIView
—for historical reasonsUIBarItem
inherits fromNSObject
—this craziness continues (as of this writing, iOS 8.2 and counting ... )The best answer in this thread is obviously @KennyTM's. Don't be silly and use the private API to find the view.
Here's a oneline Swift solution to get an
origin.x
sorted array (like Kenny's answer suggests):The array is now
origin.x
sorted with theUIBarButtonItem
frames.(If you feel the need to read more about other people's struggles with UIBarButtonItem, I recommend Ash Furrow's blog post from 2012: Exploring UIBarButtonItem)
我能够通过传递工具栏和
来让 Werner Altewischer 的 WEpopover 工作
UIBar按钮:
Mod 位于 WEPopoverController.m 中
I was able to get Werner Altewischer's WEpopover to work by passing up the toolbar along with the
UIBarButton:
Mod is in WEPopoverController.m
您可以从 UINavigationBar 视图中获取它。 navigationBar 是一个 UIView,有 2 或 3 个自定义 子视图 对于栏上的部件。
如果您知道 UIBarButtonItem 当前显示在右侧导航栏中,则可以从导航栏的子视图数组中获取其框架。
首先,您需要 navigationBar,您可以从 navigationController 您可以从 UIViewController 获取。然后找到最右边的子视图:
我还没有编译这段代码,所以 YMMV。
You can get it from the UINavigationBar view. The navigationBar is a UIView which has 2 or 3 custom subviews for the parts on the bar.
If you know that the UIBarButtonItem is currently shown in the navbar on the right, you can get its frame from navbar's subviews array.
First you need the navigationBar which you can get from the navigationController which you can get from the UIViewController. Then find the right most subview:
I haven't compiled this code so YMMV.
这不是最好的解决方案,从某些角度来看,这不是正确的解决方案,我们不能这样做,因为我们隐式访问
UIBarBattonItem
内部的对象,但您可以尝试执行以下操作:我得到的下一个结果:
This is not the best solution and from some point of view it's not right solution and we can't do like follow because we access to object inside
UIBarBattonItem
implicitly, but you can try to do something like:as result i got next:
在实现此代码之前,请务必在 Applition 委托
application:didFinishLaunchingWithOptions:
方法中调用[window makeKeyAndVisible]
!Before implement this code, be sure to call
[window makeKeyAndVisible]
in your Applition delegateapplication:didFinishLaunchingWithOptions:
method!我是这样处理的:
I handled it as follows: