Search code examples
iosobjective-cuibuttonuipickerviewuiactionsheet

Adding UIBarButtonItem to UIActionSheet navigation bar (UIDatePicker)


I created UIActionSheet and added a UIPickerView on top of it. Now, I want to add two button on the right and left of the UIActionSheet navigation bar title. How can I do that?

This is what I did so far:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Select your birthday date:" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
    [actionSheet showInView:self.view];
    [actionSheet setFrame:CGRectMake(0, 100, 570, 383)];
}

I'm trying to add the UIButton in this way, but I can't figure out why I can't see it on the view. When I check the UIActionSheet subviews, he is there (maybe somewhere under).

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 50, 50, 216)];

    //  Configure picker...
    [pickerView setDatePickerMode:UIDatePickerModeDate];
    [pickerView setTag:ACTION_SHEET_PICKER_TAG];

    //  Add picker to action sheet
    [actionSheet addSubview:pickerView];

    //  Add button to the action sheet
    UIButton *buttonCancel = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonCancel setFrame:CGRectMake(5, 100, 200, 200)];
    [buttonCancel setTitle:@"close" forState:UIControlStateNormal];
    [buttonCancel setBackgroundColor:[UIColor redColor]];
    [actionSheet addSubview:buttonCancel];

    //  Gets an array af all of the subviews of our actionSheet
    NSArray *subviews = [actionSheet subviews];
    for (id button in subviews) {
        if ([button isKindOfClass:[UIButton class]]) {
            [button setUserInteractionEnabled:NO];
            [button setHidden:YES];
        }
    }
}

This is how it looks like:

enter image description here

I figure out of there is a way to reach the UIActionSheet navigation bar or toolbar (where the title is located) and add button on top.

Thanks in advance.


According to people answer's I've managed to create a better picker view that is presented the same, but handles and build way much better. I've created a UIViewController and added a UIDatePicker and a UIToolBar with buttons on top. Than called it like that:

- (IBAction)userDidClickedOnSelectDate:(id)sender
{
    CustomDatePicker *customPicker = [[CustomDatePicker alloc] initWithNibName:@"CustomDatePicker" bundle:nil];
    customPicker.delegate = self;
    customPicker.view.backgroundColor = [UIColor clearColor];
    self.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:customPicker animated:YES completion:nil];
}

I alsvo created a Delegate that tells me when cancel and done buttons were clicked:

- (void)datePickerDidCancelled:(CustomDatePicker *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)datePicker:(CustomDatePicker *)picker dateDidSelected:(NSDate *)date
{
    NSLog(@"selectedDate: %@", date);
    [self dismissViewControllerAnimated:YES completion:nil];
}

Solution

  • Per the documentation, you shouldn't add subviews to UIActionSheets.

    UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy. If you need to present a sheet with more customization than provided by the UIActionSheet API, you can create your own and present it modally with presentViewController:animated:completion:.