Search code examples
iphoneuipopovercontrollermpmediapickercontroller

UIPopoverController forces iPad into Portrait orientation


I think the issue here is that I'm trying to call a mediaPicker and that doesn't support other orientations...

Does anyone have a fix for this?

Here is my current code:

- (IBAction)openMediaPicker:(id)sender {
    MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeAnyAudio];
mediaPicker.delegate = self;
mediaPicker.allowsPickingMultipleItems = YES; // this is the default
    mediaPicker.modalPresentationStyle = UIModalPresentationPageSheet;
    //mediaPicker.prompt = @"Select items to play";
[self presentModalViewController:mediaPicker animated:YES];
[mediaPicker release];

    // Init a Navigation Controller, using the MediaPicker as its root view controller
    UINavigationController *theNavController = [[UINavigationController alloc] initWithRootViewController:mediaPicker];
    [theNavController setNavigationBarHidden:YES];

    // Init the Popover Controller, using the navigation controller as its root view controller
    popoverController = [[UIPopoverController alloc] initWithContentViewController:theNavController];

    // Make a rect at the size and location of the button I use to invoke the popover
   CGRect popOverRect = chooseMusicButton.frame;

    // Specify the size of the popover
    CGSize MySize = CGSizeMake(520.0, 720.0);

    [popoverController setPopoverContentSize:MySize animated:YES];

    // Display the popover
    [popoverController presentPopoverFromRect:popOverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popoverController.delegate = self;
}

Solution

  • This code is overly complicated. First you present the media picker modally, then you present it as a popover; why? In the popover, you stuff it into a navigation controller before presenting it; why? Presenting a media picker on iPad is much simpler than that:

    MPMediaPickerController* picker = 
        [[[MPMediaPickerController alloc] init] autorelease];
    picker.delegate = self;
    UIPopoverController* pop = 
            [[UIPopoverController alloc] initWithContentViewController:picker];
    self.currentPop = pop;
    [pop presentPopoverFromRect:[sender bounds] inView:sender
            permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    [pop release];
    

    That works in any orientation and even survives rotation while the popover is showing.