Question

I'm not great at creating views in code yet so this is probably a simple question.

In my iPad app I have this image picker and I would like to present it in a 500x500 modal view centred on screen OR in a formsheet centre-screen:

_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_imagePicker.allowsEditing = NO;
_imagePicker.navigationBar.tintColor = [UIColor redColor];
_imagePicker.navigationBar.backgroundColor = [UIColor greenColor];

Now what do I do to both create the modal view or formsheet and present the image picker inside it?

(I've looked through many examples and Q&A and just can't find a simple answer. Thanks for your help!)

Was it helpful?

Solution

Form sheet is a modal presentation type. You change that by modifying the modalPresentationStyle property of a view controller before presentation.

_imagePicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:_imagePicker animated:YES completion:nil];

However, if you read the documentation of UIImagePickerController, you will see that Apple requires you to present the view controller inside a popover.

So:

if([_popoverController popoverVisible])
{
    [_popoverController dismissPopoverAnimated:NO];
    _popoverController = nil;
}
_popoverController = [[UIPopoverController alloc] initWithContentViewController:_imagePicker];
[_popoverController setDelegate:self];
[_popoverController presentPopoverFromBarButtonItem:_button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top