Iphone 一半的UIButton对触摸事件没有响应

Iphone 一半的UIButton对触摸事件没有响应,iphone,ios,Iphone,Ios,我有一个ui视图——我正在使用它作为临时工具栏。它包含几个ui按钮s。(我想是六个)水平排列 当我单击第五个按钮的左半部分时,它将只响应[TouchUpInside]事件,而其他所有按钮都正常工作 我已经疯了,确保没有任何其他的观点重叠,等等-一切似乎都好 你知道如何进一步调查吗?有没有办法查看“事件检查器”以查看触摸消息的去向?您可以尝试将此技术与NSLog()结合使用来记录事件并了解可能发生的情况。我不确定是否有更好的方式记录事件: 最有可能的是,另一半被透明视图遮挡。请参见按钮的所有同级视

我有一个
ui视图
——我正在使用它作为临时工具栏。它包含几个
ui按钮
s。(我想是六个)水平排列

当我单击第五个按钮的左半部分时,它将只响应[
TouchUpInside
]事件,而其他所有按钮都正常工作

我已经疯了,确保没有任何其他的观点重叠,等等-一切似乎都好


你知道如何进一步调查吗?有没有办法查看“事件检查器”以查看触摸消息的去向?

您可以尝试将此技术与NSLog()结合使用来记录事件并了解可能发生的情况。我不确定是否有更好的方式记录事件:


最有可能的是,另一半被透明视图遮挡。请参见按钮的所有同级视图的框架:

for (UIView *v in myButton.superview.subviews){
    NSLog(NSStringFromCGRect(v.frame);
}
并查看按钮上方的任何帧(数组从下到上排列)是否与它重叠


如果没有,请查看整个窗口是否有覆盖按钮的视图。

问题如下:

// Run these four lines just once in the simulator to save the original info 
// light button image to disk and then eliminate these four lines and create the
// custom button with the image added to the project. Note that it may be a 1x or
// 2x image depending on the simulator device. So run it on both to get two
// versions (changing the name of course).
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIImage *img = [button imageForState:UIControlStateNormal];
NSData *imgData = UIImagePNGRepresentation(img);
BOOL saved = [imgData writeToFile:@"/Users/Shared/InfoLight.png" atomically:NO];
“第六个”按钮是“信息灯”类型的按钮。出于某种奇怪的原因——尽管这个按钮的边界明显超出了令人不快的“第五个”按钮的边界,“信息灯”按钮似乎在其边界框之外获得了一些触动

当我:

  • 将“信息灯”按钮移离“第五个”按钮更远
  • -或-

  • 将“信息灯”按钮更改为常规(圆形矩形)按钮
  • …问题消失了

    请参见此图中最右侧的两个按钮:


    我想您的superview边界可能有点太小了?尝试将superview背景颜色更改为红色或一些鲜艳的颜色,并检查其边界

    我也遇到了这个奇怪的问题,在努力理解问题的本质之后,我偶然看到了这篇文章。谢谢你的建议!我知道这是个老问题,但这就是我如何从两个方面解决这个问题的。第一个是最初创建system info light按钮,只需检索按钮图像并使用该图像创建自定义按钮,而忽略初始按钮。这样做的好处是,你总是能得到最新的信息灯图形,以防它在操作系统中发生变化。缺点是新版本的操作系统可能会在某个时候决定不以这种方式提供映像

    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
    UIImage *img = [button imageForState:UIControlStateNormal];
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:img forState:UIControlStateNormal];
    button.showsTouchWhenHighlighted = YES;
    [button addTarget:self action:@selector(infoSelected:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    [barItems addObject:barButton];   // NSMutableArray of tab bar items
    
    第二种方法,我决定使用它,因此我知道它是可用的,并且在操作系统图形更改的情况下与我的闪屏艺术相匹配,就是采用相同的技术,将原始PNG保存到磁盘,并将其作为按钮的自定义图像添加到项目中

    // Run these four lines just once in the simulator to save the original info 
    // light button image to disk and then eliminate these four lines and create the
    // custom button with the image added to the project. Note that it may be a 1x or
    // 2x image depending on the simulator device. So run it on both to get two
    // versions (changing the name of course).
    UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
    UIImage *img = [button imageForState:UIControlStateNormal];
    NSData *imgData = UIImagePNGRepresentation(img);
    BOOL saved = [imgData writeToFile:@"/Users/Shared/InfoLight.png" atomically:NO];
    

    我注意到,在选择infoLight类型时,该按钮无法再与之交互。通过选中复选框“用户交互启用”,我解决了这个问题

    从这个输出来看一切都正常-所有视图间隔45像素,所有按钮宽度37像素-没有重叠。不过谢谢你的提示!这是迄今为止调试问题的最佳方法。如果您使用的是故事板,还可以使用“视图调试层次结构”工具查看当前视图层的布局,以查看顶部是否有透明的内容