36

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

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

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

我的问题是这个-

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

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

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

我的问题是这个-

有人可以告诉我如何使导航栏不为其按钮抓取那些额外的 5+ 像素吗?

非常感谢 ;)

4

8 回答 8

6

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

//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];
于 2014-05-27T02:59:21.747 回答
3

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

UIBarButtonItem 不会扩展 UIResponder 但 UINavigationBar 会!

因此,如果您将 UINavigationBar 子类化并在您的应用程序中使用此子类,也许您可​​以捕获触摸的坐标并检查它们是否适合您,然后决定应用导航栏操作或按钮操作(通过一些自定义重定向) .

于 2011-02-16T17:13:47.543 回答
2

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

于 2010-02-16T12:17:43.933 回答
1

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

于 2011-02-16T14:27:47.137 回答
1

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

于 2011-02-23T17:49:55.560 回答
1

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

我创建了一个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
于 2016-05-22T17:14:57.640 回答
0

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

于 2014-09-18T15:11:00.823 回答
0
    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)
于 2015-07-18T17:45:19.870 回答