【问题标题】:UINavigationBar UIBarButtonItems much larger click area than requiredUINavigationBar UIBarButtonItems 比所需的点击区域大得多
【发布时间】:2010-02-16 09:59:23
【问题描述】:

希望有人可以帮助我-我在网上搜寻答案却找不到答案-

添加到 UINavigationBar 的 UIBarButtonItems 具有比所需更大的点击区域- 例如,打开任何项目,您有一个带有按钮的导航栏-单击按钮末尾和导航栏标题之间​​的任意位置-当您显然没有单击按钮时,按钮单击-

也试试这个——点击导航栏下方,按钮下方,按钮点击导航栏下方约 5+ 像素——

我的问题是这个-

我已经向 tableview 添加了一个带有按钮的自定义标题-但是当我单击标题中的按钮时,UINavigationBar 按钮会触发 5+ 像素而不是 tableview 标题中的按钮-

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

它几乎就像导航栏在其下方保留了大约 5+ 像素作为点击空间-

我的问题是这个-

谁能告诉我如何让导航栏不为它的按钮抓取那些额外的 5+ 像素?

非常感谢;)

【问题讨论】:

  • 你找到解决办法了吗?
  • 您是否尝试将导航按钮替换为您自己的 UIButton?
  • 情况变得更糟——对于任一侧的单个 left/rightBarButtonItem,水平“按下”区域从任一侧扩展约 100 个像素——即 (0-100,y) 注册一个左侧点击,同时 (220-320,y) 向右!

标签: iphone uinavigationbar uibarbuttonitem


【解决方案1】:

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

//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 子类化并在应用程序中使用此子类,也许您可​​以捕获触摸坐标并检查它们是否适合您,然后决定应用导航栏操作或按钮操作(通过某些自定义重定向)。

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 感谢您的回复-是的,我知道苹果这样做是为了方便拇指胖的人使用-但我仍然想将其关闭-即使我在tableview header 更大,问题是,如果用户正确地点击了我的一个按钮的顶部,它会触发错误的事件——必须有一个关于如何关闭这个“胖拇指”“功能”的答案——谢谢
    【解决方案4】:

    据我所知,关闭是不可能的。如果导航栏上有其他按钮,这些点击空间不会发生冲突,但如果导航栏正下方有按钮,中间没有任何空间,那么你就不走运了。考虑在标题及其按钮中使用小填充作为解决方案。

    【讨论】:

      【解决方案5】:

      当您提交到应用商店时,尝试解决 UINavigation Bar 填充可能会给您带来麻烦。将填充添加到自定义标题会更容易。作为一个“胖拇指”,我学会了欣赏 HIG。

      【讨论】:

        【解决方案6】:

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

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

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

        标题:

        @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
        

        【讨论】:

          【解决方案7】:

          这可以直接从情节提要中完成。将 UIView 拖入每个导航项,将其背景设置为 clearColor,并调整其大小。将一个按钮拖入每个 UIView 并调整它们的大小以匹配。

          【讨论】:

            【解决方案8】:
                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)
            

            【讨论】:

              猜你喜欢
              • 2018-02-11
              • 1970-01-01
              • 2014-09-10
              • 2010-10-22
              • 2015-07-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多