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):
hitTest:withEvent:method on theUIWindowobject of your app.hitTest:withEvent:on each of its children, and these each callhitTest:withEvent:on their children if necessary.hitTest:withEvent:is called, it first looks whether the coordinate is within its own bounds (IIRC). If it isn’t, it returnsnil. Otherwise it callshitTest:withEvent:on its children.The
hitTest:withEvent:documentation explicitly says: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.