简体   繁体   中英

A UIButton that I can not touch

I made a button above a pickerView to dismiss it. and I think I made it, here the the code that initiates the pickerView as well as its dismiss button.

    UIPickerView *tempPickerView = [[[UIPickerView alloc] initWithFrame:
        CGRectMake(kPickerViewX, kPickerViewY, kPickerViewWidth, kPickerViewHeight)] autorelease];
    tempPickerView.showsSelectionIndicator = YES;
    tempPickerView.delegate = self;
    tempPickerView.dataSource = self;
    UIButton *pickerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, -32, 52, 32)];
    [pickerButton setBackgroundImage:[UIImage imageNamed:@"week.png"] 
         forState:UIControlStateNormal];
    [pickerButton addTarget:self action:@selector(hidePicker) 
         forControlEvents:UIControlEventTouchUpInside];
    [tempPickerView addSubview:pickerButton];
    [tempPickerView bringSubviewToFront:pickerButton];
    [pickerButton release];

    [self.view addSubview:tempPickerView];
    return tempPickerView;

and it looks like this:

在此处输入图片说明

that button is at the upper left cornor of the pickerView, forget about the "week", it is just a temporary name.

Problem is, I can't touch this button, everytime I tried to touch it, I just directly touched things behind it such as the talbeViewCell.

Is is because I added the button outside of the pickerView's frame?

I mean the coordinate of the button's origin is (0, -32),

so that it would be displayed above the pickerView, is there any problems with the coordinate?

Can anyone tell how to fix this problem?

Thanks a lot and a lot!!!

The problem is that you have placed the button outside the bounds of its parent view. iOS now does this (simplified, not 100% correct but you get the deal):

  • It notices a touch occurred. It calculates the coordinates of that touch.
  • It then tries to find out which view was touched. To do so, it calls the hitTest:withEvent: method on the UIWindow object of your app.
  • That in turn calls hitTest:withEvent: on each of its children, and these each call hitTest:withEvent: on their children if necessary.
  • When a view's hitTest:withEvent: is called, it first looks whether the coordinate is within its own bounds (IIRC). If it isn't, it returns nil . Otherwise it calls hitTest:withEvent: on its children.

The hitTest:withEvent: documentation explicitly says:

Points that lie outside the receiver's bounds are never reported as hits, even if they actually lie within one of the receiver's subviews.

The problem now is that your button is outside of its parent view. So hitTest:withEvent: determines that the touch coordinate is not inside the bounds of the picker view and never bothers to ask its child (the button) whether it might be hit.

You could subclass the picker and override the hitTest:withEvent: method to "fix" this, but the real fix is to move the button so its a sibling of the picker, not a child.

I think this may be caused by UIPickerView 's hit-testing method. It may return NO from pointInside:withEvent: if the touch is outside its bounds. The header file (UIView.h) comment says

default returns YES if point is in bounds

Since you place the button outside the bounds of UIPickerView , that may well be the reason.

As a workaround, you can insert the button into the picker's superview.

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