1

I'm updating my app to iOS 8 and trying to recreate the UIPickerView inside a UIActionSheet with UIAlertController as described here. Using the answer from Nada Gamal, I was able to recreate the functionality, but the UIPickerView only responds within the tab bar (even though the tab bar is hidden behind the picker view). The tab bar controller is the root view controller.

-(void)loadPickerView{
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;
    pickerView.backgroundColor = [UIColor whiteColor];
    [pickerView selectRow:pickerViewRow inComponent:0 animated:NO];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    pickerToolbar.tintColor = self.navigationController.navigationBar.tintColor;

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet)];
    [barItems addObject:doneBtn];

    [pickerToolbar setItems:barItems animated:YES];

    UIAlertController * searchActionSheet=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

    [ searchActionSheet.view setBounds:CGRectMake(8, 208, self.view.frame.size.width, pickerView.frame.size.height+pickerToolbar.frame.size.height)];

    [searchActionSheet.view addSubview:pickerToolbar];
    [searchActionSheet.view addSubview:pickerView];
    [self presentViewController:searchActionSheet animated:YES completion:nil];
}

The presentViewController in the last line seems to be the issue. I've tried many different ways to get the pickerView to respond outside the tab bar area, including:

  • self
  • self.tabBarController
  • self.navigationController (pickerView doesn't show up at all)
  • [[UIApplication sharedApplication] keyWindow].rootViewController

The other option is to make a separate view controller for this, but I'm concerned that won't work either because UIAlertController is a subclass of UIViewController, but perhaps this is an issue with adding subviews to UIAlertController.

Community
  • 1
  • 1
scottoliver
  • 536
  • 1
  • 4
  • 19

0 Answers0