我在我的应用上实现了SWReveal控件 . 为了解决这个问题,我有一个带有自定义视图的UIBarButtonItem . 好吧,一切都工作正常,但我注意到,即使点击远离图像(大约44px宽的图像......并点击高达100px),我也可以发射事件 .
我一直在寻找,并找到了我找到的最佳解决方案(定义自定义按钮并使用“initWithCustomView”实例化UIBarButtonItem)
这是我之前的代码:
UIBarButtonItem *revealButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"reveal-icon"] style:UIBarButtonItemStylePlain target:_revealController action:@selector(revealToggle:)];
self.navigationItem.leftBarButtonItem = revealButtonItem;
而代码与“解决方案”:
//create the image for your button, and set the frame for its size
UIImage *image = [UIImage imageNamed:@"reveal-icon"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
//init a normal UIButton using that image
UIButton* button = [[UIButton alloc] initWithFrame:frame];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:_revealController action:@selector(revealToggle:) forControlEvents:UIControlEventTouchUpInside];
//finally, create your UIBarButtonItem using that button
UIBarButtonItem* revealButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = revealButtonItem;
有了这个后面的代码,我知道我有一个正确大小的按钮(我设置了backgroundColor,并检查了按钮的正确大小),但是当点击远离按钮时仍然触发动作 .
有任何想法吗?
先感谢您 .
4 回答
尝试使用
button.clipsToBounds = YES;
,但我必须同意其他评论,根据Apple的iOS Human Interface Guidelines,每个可点击区域应至少为44x44 .自定义视图路径似乎对我有用,但我确实找到了另一种解决方案,如果这对您不起作用,您可以尝试 . 似乎可点击区域只会延伸到远远超过用户可以点击的东西 . 我有一个 Headers 视图,用户可以点击以执行某些操作,因此我的leftbarbuttonitem的可点击区域停在那里 . 但我也尝试添加另一个uiview,userinteractionenabled设置为yes只是略微在leftbarbuttonitem右侧,这似乎阻止该区域扩展 . 我只能使用两行标准代码而不是自定义代码 . 不确定这是否适合你
你可以试试这个
button.clipsToBounds = YES;
这可能对你有所帮助 .