【问题标题】:UINavigationItem Back Button touch area too largeUINavigationItem 后退按钮触摸区域太大
【发布时间】:2011-04-24 04:37:26
【问题描述】:

在以下屏幕截图中,如果我从“可用信息亭”中单击“v”,这将启动后退按钮的操作...(不是第二个“a”)。

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

有什么想法吗?

谢谢。

【问题讨论】:

  • 我现在也遇到了同样的问题,你找到解决办法了吗?
  • 不,对不起...我在许多应用程序中发现了这个错误...:o

标签: objective-c xcode back-button uinavigationitem


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    这是我的类别:

    @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 的 swizzle 实现

    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 指南,如果在我将应用程序提交到应用商店后,我会尝试更新帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多