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

时间:2010-02-16 09:59:24

标签: iphone uinavigationbar uibarbuttonitem

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

添加到UINavigationBar的UIBarButtonItem具有比所需更大的点击区域 - 例如,打开任何项目,你有一个带按钮的导航栏,点击按钮结束和导航栏标题之间​​的任何地方 - 按钮点击,当你明显没有点击按钮时 -

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

我的问题是这个 -

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

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

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

我的问题是这个 -

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

非常感谢;)

8 个答案:

答案 0 :(得分: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];

答案 1 :(得分:3)

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

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

因此,如果您继承UINavigationBar并在您的应用程序中使用此子类,也许您可​​以捕获触摸的坐标并检查它们是否对您好,然后决定应用导航栏操作或按钮操作(由某些人自定义重定向)。

答案 2 :(得分:2)

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

答案 3 :(得分:1)

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

答案 4 :(得分:1)

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

答案 5 :(得分:1)

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

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

在我的例子中,导航栏子类是通过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

答案 6 :(得分:0)

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

答案 7 :(得分: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)
相关问题