简体   繁体   中英

Half of UIButton not responding to touch events

I have a UIView - which I am using as a makeshift toolbar. It contains several UIButton s. (Six, I think) in a horizontal row.

The fifth button over will only respond to [ TouchUpInside ] events when I click the left half of it - whereas all the other buttons work correctly.

I have gone crazy making sure there weren't any other views overlapping it, etc - and everything seems okay.

Any idea of how to investigate further? Is there any way to see an "event inspector" to see where the touch messages may be going?

Most likely, the other half is covered by a transparent view that obstructs it. See the frames of all sibling views of the button:

for (UIView *v in myButton.superview.subviews){
    NSLog(NSStringFromCGRect(v.frame);
}

And see if any of the frames above the button (the array is ordered from bottom to top) overlap with it.

If they don't, see if the whole window has any views covering the button.

The problem was as follows:

The "sixth" button was a "Info Light" type button. For some odd reason - even though the bounds of this button were clearly outside of the bounds of the offending "fifth" button - the "info light" button seems to acquire touches a bit outside it's bounding box.

When I either:

  1. Moved the "Info Light" button further away from the "fifth" button

-or-

  1. Changed the "Info Light" button to a regular (Round Rect) button

...the problem went away!

See the two rightmost buttons in this image:

替代文字

I guess your superview bounds might be a bit too small? Try to change your superview background color to red or some vivid color and check its bounds.

Just ran into this strange problem as well and after struggling with understanding the nature of the problem, ran across this post. Thanks for the helpful tip! I know it's an old question but this is how I was able to work around it in two ways. The first was to initially create the system info light button and simply retrieve the button image and create a custom button with that image, ignoring the initial button. The advantage to this is that you always get the latest info light graphic in case it changes in the OS. The disadvantage being that a new version of the OS might decide at some point to not make the image available in this way.

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

The second approach, which I decided to use, so that I knew it would be available, and also to match my splash screen art in case the OS graphic changed, was to follow the same technique and simply save the original PNG to disk and add it to the project as a custom image for the button.

// 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];

I noticed that upon selecting the infoLight type, the button could no longer be interacted with. By checking the checkbox "User Interaction Enabled" I solved the problem.

Well you could try to use this technique with NSLog() to log events and get a sense of what might be happening. I'm not sure if there's a better way to log events:

Observing pinch multi-touch gestures in a UITableView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM