2

For some reasons I restrict my app to work only on landscape mode, but strangely when I open iPod music library from my app, it will always jump out in portrait mode.

See the following code, I am confused whether it is system default behavior? How can I tell it to come out in landscape mode (keep consistent with other UI)? Thanks.

- (IBAction)getMusic:(id)sender {
    MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];
    picker.delegate                     = self;
    picker.allowsPickingMultipleItems   = YES;
    picker.prompt                       = NSLocalizedString (@"Add songs to play",nil);

    [self presentModalViewController: picker animated: NO];
    [picker release];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationIsLandscape(interfaceOrientation);;
    } else {
        return YES;

    }
}
jianhua
  • 1,011
  • 1
  • 12
  • 28

2 Answers2

5

Just add the view of MPMediaPickerController to a viewcontroller's view that supports orientations that you want. (tested in iOS 6.1 SDK)

mediaPickerController = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];

mediaPickerController.delegate = self;
mediaPickerController.allowsPickingMultipleItems = NO;
mediaPickerController.prompt = @"Select songs to edit";

//[self presentViewController:mediaPicker animated:YES completion:^{}];
[self.view addSubview:mediaPickerController.view];

enter image description here

In addition, in case of iOS 7 you should set media picker's frame as below.

mediaPickerController = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny];

mediaPickerController.delegate = self;
mediaPickerController.allowsPickingMultipleItems = NO;
mediaPickerController.prompt = @"Select a song to edit";

// in case of iOS 7
mediaPickerController.view.frame = CGRectMake(0, 0, 568, 320);

[self.view addSubview:mediaPickerController.view];

enter image description here

alones
  • 2,848
  • 2
  • 27
  • 30
1

From the MPMediaPickerController Class reference:

Notes: The MPMediaPickerController class supports portrait mode only. This class does support subclassing. The view hierarchy for this class is private; do not modify the view hierarchy.

There are posts here and elsewhere that describe workaround, but I can't vouch for them.

Community
  • 1
  • 1
ari gold
  • 2,074
  • 3
  • 25
  • 51
  • I should mention that if you push it onto a NavigationController, it will show up in landscape. That is, you can get around the note in Apple's class reference. – ari gold Jan 12 '13 at 23:52
  • Plz see the following my answer. Just add the view of MPMediaPickerController to the parent viewcontroller's view. – alones May 27 '13 at 14:07
  • what's wrong with the answer all of a sudden, oh noble downvoter? – ari gold Sep 26 '13 at 16:16