Search code examples
iphoneiosipaduipickerviewsubclass

UIToolbar above custom UIPickerView not receiving touches


I've subclassed UIPickerView to add some a little more functionality(I'm 99% sure it has nothing to do with this question). In drawRect I added a toolbar to make dismissing the toolbar a little easier, the problem is, neither the UIToolbar, or the UIBarButtonItem inside the toolbar receive touches. It's almost as if the view is "invisible" in that the touches are forwarded to the view behind it(a UITableView). I know I could just make a "control" view that holds both the picker an a toolbar. But I just wanted to know if there was any way to do this without creating another view?

Here's my drawRect code:

- (void)drawRect:(CGRect)rect
{

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

        pickerToolbar = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, self.frame.size.width,  40)];


        UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target: self.delegate action: closeAction];

        [pickerToolbar setItems: [NSArray arrayWithObject: closeButton]];

        [self addSubview: pickerToolbar];
    }
}

Here's a photo:

My custom UIPicker


Solution

  • Never, never do this. You are adding a new subview every time the picker is drawn!

    If your picker view is the input view of a text field, simply create a toolbar and add it as the input accessory view. It will then appear above the picker for you. This is the standard way to achieve this behaviour.