【发布时间】:2011-02-28 23:26:53
【问题描述】:
UIBarButtonItem 没有扩展 UIView,所以没有什么像框架属性。
但是有什么方法可以让我获得相对于应用程序UIWindow 的CGRect 框架?
【问题讨论】:
标签: iphone objective-c cocoa-touch uikit
UIBarButtonItem 没有扩展 UIView,所以没有什么像框架属性。
但是有什么方法可以让我获得相对于应用程序UIWindow 的CGRect 框架?
【问题讨论】:
标签: iphone objective-c cocoa-touch uikit
你喜欢使用private APIs 吗?如果是,
UIView* view = thatItem.view;
return [view convertRect:view.bounds toView:nil];
当然,在瞄准 AppStore 时没有人想要这个。一个更不可靠的方法,也使用了未记录的特性,但会通过Apple的测试,是循环遍历子视图以查找相应的按钮项。
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 数组中的条形项的索引,删除所有空白项后。这假设按钮按升序排列,但可能不是。更可靠的方法是排序 buttons 数组以增加.origin.x 值。当然,这仍然假设条形按钮项必须继承 UIControl 类,并且是工具栏/导航栏的直接子视图,这又可能不是。
如您所见,在处理未记录的功能时存在很多不确定性。但是,您只是想在手指下弹出一些东西对吗? UIBarButtonItem 的.action 可以是表单的选择器:
-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event;
注意 event 参数——你可以通过
获得触摸的位置[[event.allTouches anyObject] locationInView:theWindow]
或按钮视图
[[event.allTouches anyObject] view]
因此,您无需迭代子视图或使用未记录的功能来完成您想做的事情。
【讨论】:
[[event.allTouches anyObject] 将无法正常工作。相反,您希望获得与 UITouchPhaseEnded 状态相对应的触摸,如:for( UITouch* touch in [event allTouches] ) { if( [touch phase] == UITouchPhaseEnded ) { view = [touch view]; } }
我没有看到这个选项发布(在我看来它更简单),所以这里是:
UIView *barButtonView = [barButtonItem valueForKey:@"view"];
【讨论】:
在 iOS 3.2 中,有一种更简单的方法可以从工具栏按钮显示操作表弹出框。只需做这样的事情:
- (IBAction)buttonClicked:(UIBarButtonItem *)sender event:(UIEvent *)event
{
UIActionSheet *popupSheet;
// Prepare your action sheet
[popupSheet showFromBarButtonItem:sender animated:YES];
}
【讨论】:
这是我用于我的 WEPopover 项目的实现:(https://github.com/werner77/WEPopover):
@implementation UIBarButtonItem(WEPopover)
- (CGRect)frameInView:(UIView *)v {
UIView *theView = self.customView;
if (!theView.superview && [self respondsToSelector:@selector(view)]) {
theView = [self performSelector:@selector(view)];
}
UIView *parentView = theView.superview;
NSArray *subviews = parentView.subviews;
NSUInteger indexOfView = [subviews indexOfObject:theView];
NSUInteger subviewCount = subviews.count;
if (subviewCount > 0 && indexOfView != NSNotFound) {
UIView *button = [parentView.subviews objectAtIndex:indexOfView];
return [button convertRect:button.bounds toView:v];
} else {
return CGRectZero;
}
}
@end
【讨论】:
只要UIBarButtonItem(和UITabBarItem)不继承自UIView——由于历史原因UIBarItem继承自NSObject——这种疯狂仍在继续(在撰写本文时,iOS 8.2 和计数.. .)
这个帖子的最佳答案显然是@KennyTM's。别傻了,用私有API找视图吧。
这是获取origin.x 排序数组的单行Swift 解决方案(如Kenny's answer 建议的那样):
let buttonFrames = myToolbar.subviews.filter({
$0 is UIControl
}).sorted({
$0.frame.origin.x < $1.frame.origin.x
}).map({
$0.convertRect($0.bounds, toView:nil)
})
数组现在是 origin.x 与 UIBarButtonItem 帧排序。
(如果你觉得有必要阅读更多关于其他人对 UIBarButtonItem 的挣扎,我推荐 Ash Furrow's 2012 年的博文:Exploring UIBarButtonItem)
【讨论】:
我能够让 Werner Altewischer 的 WEpopover 通过向上传递工具栏和
UIBar按钮:
Mod在WEPopoverController.m中
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item toolBar:(UIToolbar *)toolBar
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated
{
self.currentUIControl = nil;
self.currentView = nil;
self.currentBarButtonItem = item;
self.currentArrowDirections = arrowDirections;
self.currentToolBar = toolBar;
UIView *v = [self keyView];
UIButton *button = nil;
for (UIView *subview in toolBar.subviews)
{
if ([[subview class].description isEqualToString:@"UIToolbarButton"])
{
for (id target in [(UIButton *)subview allTargets])
{
if (target == item)
{
button = (UIButton *)subview;
break;
}
}
if (button != nil) break;
}
}
CGRect rect = [button.superview convertRect:button.frame toView:v];
[self presentPopoverFromRect:rect inView:v permittedArrowDirections:arrowDirections animated:animated];
}
【讨论】:
-(CGRect) getBarItemRc :(UIBarButtonItem *)item{
UIView *view = [item valueForKey:@"view"];
return [view frame];
}
【讨论】:
您可以从 UINavigationBar 视图中获取它。 navigationBar 是一个 UIView,它有 2 或 3 个自定义 subviews 用于栏上的部分。
如果您知道 UIBarButtonItem 当前显示在右侧的导航栏中,则可以从导航栏的子视图数组中获取其框架。
首先你需要navigationBar,你可以从navigationController获得,你可以从UIViewController获得。然后找到最右边的子视图:
UINavigationBar* navbar = curViewController.navigationController.navigationBar;
UIView* rightView = nil;
for (UIView* v in navbar.subviews) {
if (rightView==nil) {
rightView = v;
} else if (v.frame.origin.x > rightView.frame.origin.x) {
rightView = v; // this view is further right
}
}
// at this point rightView contains the right most subview of the navbar
这段代码我还没编译所以YMMV。
【讨论】:
这不是最好的解决方案,从某些角度来看,这不是正确的解决方案,我们不能像 follow 一样做,因为我们会隐式访问 UIBarBattonItem 内的对象,但您可以尝试执行以下操作:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[button setImage:[UIImage imageNamed:@"Menu_Icon"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(didPressitem) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = item;
CGPoint point = [self.view convertPoint:button.center fromView:(UIView *)self.navigationItem.rightBarButtonItem];
//this is like view because we use UIButton like "base" obj for
//UIBarButtonItem, but u should note that UIBarButtonItem base class
//is NSObject class not UIView class, for hiding warning we implicity
//cast UIBarButtonItem created with UIButton to UIView
NSLog(@"point %@", NSStringFromCGPoint(point));
结果我得到了下一个:
点 {289, 22}
【讨论】:
在实现此代码之前,请务必在您的 Applition 委托 application:didFinishLaunchingWithOptions: 方法中调用 [window makeKeyAndVisible]!
- (void) someMethod
{
CGRect rect = [barButtonItem convertRect:barButtonItem.customview.bounds toView:[self keyView]];
}
- (UIView *)keyView {
UIWindow *w = [[UIApplication sharedApplication] keyWindow];
if (w.subviews.count > 0) {
return [w.subviews objectAtIndex:0];
} else {
return w;
}
}
【讨论】:
UIBarButtonItem 实例没有 bounds 属性。
我是这样处理的:
- (IBAction)buttonClicked:(UIBarButtonItem *)sender event:(UIEvent *)event
{
UIView* view = [sender valueForKey:@"view"]; //use KVO to return the view
CGRect rect = [view convertRect:view.bounds toView:self.view];
//do stuff with the rect
}
【讨论】: