UINavigationItem Back Button触摸区域太大

时间:2010-10-12 08:36:30

标签: objective-c xcode back-button uinavigationitem

在下面的屏幕截图中,如果我点击“可用自助服务终端”中的“v”,则会启动后退按钮的操作...(不是第二个“a”)。

alt text

我不明白为什么,我的代码没什么特别的(这是导航控制器处理的默认后退按钮)。 我也遇到了与我做的另一个应用程序相同的错误,但我从未在其他应用程序中注意到这一点。

任何想法?

谢谢。

2 个答案:

答案 0 :(得分:8)

这不是一个错误,它在Apple应用程序中也是如此,甚至在一些(很多/所有?)按钮上也是如此。这是按钮上触摸事件的行为:触摸区域大于按钮边界。

答案 1 :(得分:1)

我需要做同样的事情,所以我最终调用UINavigationBar touchesBegan:withEvent方法并在调用原始方法之前检查触摸的y坐标。
这意味着当触摸太靠近我在导航下使用的按钮时,我可以取消它。

例如:后退按钮几乎总是捕获触摸事件而不是“第一”按钮 enter image description here

这是我的类别:

@implementation UINavigationBar (UINavigationBarCategory)
- (void)sTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
float maxY = 0;
for (UITouch *touch in touches) {
    float touchY = [touch locationInView:self].y;
    if ( [touch locationInView:self].y > maxY) maxY = touchY;
}

NSLog(@"swizzlelichious bar touchY %f", maxY);

if (maxY < 35 )
    [self sTouchesEnded:touches withEvent:event];
else 
    [self touchesCancelled:touches withEvent:event];
}
- (void)sTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
float maxY = 0;
for (UITouch *touch in touches) {
    float touchY = [touch locationInView:self].y;
    if ( [touch locationInView:self].y > maxY) maxY = touchY;
}

NSLog(@"swizzlelichious bar touchY %f", maxY);

if (maxY < 35 )
    [self sTouchesBegan:touches withEvent:event];
else 
    [self touchesCancelled:touches withEvent:event];
}

来自CocoaDev

的Mike Ash的混合实施
void Swizzle(Class c, SEL orig, SEL new)
{
Method origMethod = class_getInstanceMethod(c, orig);
Method newMethod = class_getInstanceMethod(c, new);
if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
    class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
else
    method_exchangeImplementations(origMethod, newMethod);
}

该函数调用swizzle函数

Swizzle([UINavigationBar class], @selector(touchesEnded:withEvent:), @selector(sTouchesEnded:withEvent:));
Swizzle([UINavigationBar class], @selector(touchesBegan:withEvent:), @selector(sTouchesBegan:withEvent:));

我不知道Apple是否可以使用它,它可能侵犯了他们的UI指南,如果我将应用程序提交到应用商店后,我会尝试更新帖子。