首页 文章

UINavigationBar UIBarButtonItems比所需的点击区域大得多

提问于
浏览
34

希望有人可以帮助我 - 我在网上搜索答案,但找不到 -

添加到UINavigationBar的的UIBarButtonItems比须─例如一个更大的点击区域,打开你有按钮的导航栏任何项目导通单击该按钮结束和NAV酒吧,按钮的点击 Headers 之间的任何地方,当你明显没有点击按钮时 -

也可以尝试这一点 - 点击导航栏下方,按钮下方,按钮点击导航栏下方约5个像素 -

我的问题是这个 -

我已经在桌面视图中添加了带按钮的自定义 Headers - 但是当我单击 Headers 中的按钮时,UINavigationBar按钮会触发这5个像素而不是tableview Headers 中的按钮 -

我做了一个测试,并从UINavigationBar中删除了按钮,有趣的是,对于导航栏下方的5个像素,即使导航栏中没有按钮, Headers 中的按钮也不会触发 -

它几乎就像导航栏一样保留了约5个像素作为点击空间 -

我的问题是这个 -

有人能告诉我如何使导航栏没有为其按钮抓取额外的5个像素?

非常感谢 ;)

8 回答

  • 5

    这是我找到的唯一解决方案 . 创建自定义按钮的容器:

    //Create a container for the button
    UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, 44)];
    
    //Create a smaller button
    UIButton *closeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 55, 25)];
    [closeButton setTitle:@"Cancel" forState:UIControlStateNormal];
    //center the title
    closeButton.titleEdgeInsets = UIEdgeInsetsMake(23, 0, 0, 0);
    
    [buttonContainer addSubview:closeButton];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
    
  • 2

    我不是100%肯定,但也许你可以通过UITouch课程获得触摸的位置?

    UIBarButtonItem不扩展UIResponder但是UINavigationBar会扩展!

    所以,如果你继承UINavigationBar的,并在你的应用程序中使用这个子,也许你可以捕捉触摸的坐标,并检查它们是否确定,你再决定申请或者导航栏动作或者您的按钮动作(通过一些自定义重定向) .

  • 1

    简短的回答是......不应该试图摆脱它们 . 这是关于易用性 . 顶部的导航栏往往意味着人们的点击率低于您的预期 . 总是留在那里的间隙,或者有一个足够大的击中区域,用户将手指刺向“导航栏下方”项目的中间将避开死区 .

  • 1

    据我所知,不可能关闭 . 如果导航栏上有其他按钮,那些点击空间不会发生碰撞,但是如果你的导航栏正下方有按钮,两者之间根本没有空间,那你就不走运了 . 考虑 Headers 中的小填充及其按钮作为解决方案 .

  • 0

    当您提交到应用商店时,尝试解决UINavigation Bar填充可能会让您遇到麻烦 . 将填充添加到自定义 Headers 会更容易 . 作为一个“胖子”,我学会了欣赏HIG .

  • 3

    相当古老的问题,但也许一个解决方案对其他人也有帮助......

    我创建了一个 UINavigationBar subclass ,它只覆盖了一个方法:' hitTest:withEvent: ' . 当 hitTest:withEvent 被调用时,它会检查导航栏框内是否发生了事件(pointInside:withEvent :) . 如果事件发生在外部,则 userInteractionEnabled 标志设置为 NO ,因此导航栏及其子视图将忽略该事件 .

    在我的例子中,导航栏子类是通过IB插入的,但当然也可以通过'UINavigationController initWithNavigationBarClass:toolbarClass:'插入它

    Headers :

    @interface MMMasterNavigationBar : UINavigationBar
    
    @end
    

    执行:

    @implementation MMMasterNavigationBar
    
    /*
     hitTest:withEvent:
    
     The hit area in for navigation bar button items is enlarged by default.
     Other objects directly below the navigation bar doesn't receive tap events.
     We avoid the default enlarging of the tappable area by disabling userInteraction
     when the real tap is outside the navigation bar.
    
     */
    -(UIView *)hitTest:(CGPoint)pPoint
             withEvent:(UIEvent *)pEvent {
        //FLog;
    
        if ([self pointInside:pPoint
                    withEvent:pEvent]) {
            //NSLog(@"User interaction enabled");
            self.userInteractionEnabled = YES;
    
        } else {
            //NSLog(@"User interaction disabled");
            self.userInteractionEnabled = NO;
        }
    
        return [super hitTest:pPoint
                    withEvent:pEvent];
    }
    
    @end
    
  • 0

    这可以直接从故事板完成 . 将UIView拖到每个导航项中,将其背景设置为clearColor,并调整其大小 . 将按钮拖入每个UIView并调整大小以匹配 .

  • 1
    var buttonContainer:UIView = UIView()
        buttonContainer.frame = CGRectMake(0, 0, 32, 32)
    
        var imagess:UIImage = UIImage(named: "noti@2x.png")!
    
        var closeButton:UIButton = UIButton()
        closeButton.setImage(imagess, forState: UIControlState.Normal)
        closeButton.frame = CGRectMake(10, 5, 20, 20)
        closeButton.contentMode = UIViewContentMode.Center
        closeButton.titleEdgeInsets = UIEdgeInsetsMake(20, 0, 0, 0)
    
        buttonContainer.addSubview(closeButton)
    
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: buttonContainer)
    

相关问题