UIPopoverController forces iPad into Portrait orientation
I think t开发者_JAVA技巧he 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;
}
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.
All pre-defined modal controllers support all orientations but they must be presented from the root view controller for them to behave correctly in orientation and rotation. My guess is that that the "self" in your code is not the root view controller. You may have to re-architect the code a bit to make this happen if possible.
There are other hacks I have seen to make it work without being presented from the root view controller but they all seemed to be asking for trouble such as extending UIViewController with a category to over-ride interfaceOrientation.
If you can present it from the root view controller, it would be the simplest and cleanest but I realize it is not always possible (e.g., it is inside a library you are providing to third party apps to embed).
精彩评论