2

I have a NavigationController, another controller was pushed on its stack: BNRDetailsViewController. Now, inside BNRDetailsViewController I am trying to show a popover window when pressing on a toolbar button, that would present me with UIImagePickerController (only on iPad devices). So, I tried to follow the following stackoverflow thread:

UIPopoverPresentationController on iOS 8 iPhone

But no success. If I just use their code I get an error that says, that pushing navigationController is not supported. If I try to push the newly created UIPopoverPresentationController like this: [self.navigationController pushViewController:self.imagePickerPopover animated:YES]; it crashes because UIPopoverPresentationController is not of type UIViewController, so, I guess, I can not just push it on the stack.

What would you suggest to do in this particular case?

Here is the code which I have right now that is triggered when the toolbar button is pressed:

- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

// If the device have camera, take a picture, otherwise,
// just pick from photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

imagePicker.delegate = self;

// Place image picker on the screen
//[self presentViewController:imagePicker animated:YES completion:NULL];

// Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // Create a new popover controller that will display the imagePicker
    _imagePickerPopover = [[UIPopoverPresentationController alloc] initWithPresentedViewController:imagePicker presentingViewController:self];

    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    _imagePickerPopover.delegate = self;
    _imagePickerPopover.sourceView = self.view;
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;
    _imagePickerPopover.sourceRect = frame;
   // [self.navigationController pushViewController:self.imagePickerPopover animated:YES];
}
}
Community
  • 1
  • 1
Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87
  • Basically you want to present a popover on iOS 8+. See the example code in this question: http://stackoverflow.com/q/28521583/558933 . The code presents a popover on both iPhone and iPad. If you want the popover to ONLY appear on iPad and NOT iPhone then return the appropriate `UIModalPresentationStyle` value. – Robotic Cat Nov 14 '15 at 21:38
  • Thank you!. However, I do not use storyboards, instead I have .xib file. So, I am still not sure what to do in this case. – Nikita Vlasenko Nov 14 '15 at 22:33

1 Answers1

2

Here is the code, based on your code, that will get the popover to display on the iPad.

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    imagePicker.modalPresentationStyle = UIModalPresentationPopover;
    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;

    UIPopoverPresentationController *popoverController = 
        imagePicker.popoverPresentationController;
    popoverController.sourceView = self.view;
    popoverController.sourceRect = frame;

    [self.navigationController presentViewController:imagePicker 
                               animated:YES completion:nil];
}
Daniel Zhang
  • 5,778
  • 2
  • 23
  • 28
  • OK, I got it. Just need to change one line: `popoverController.sourceView = self.view` to `popoverController.sourceView = self.toolbar` where `toolbar` was defined as a property inside the BNRDetailsViewController class: `@property (weak, nonatomic) IBOutlet UIToolbar *toolbar` – Nikita Vlasenko Nov 15 '15 at 00:50
  • Just a note: preferredContSizre replaced popoverContentSize – user523234 Mar 24 '16 at 17:45