UIPopoverController 强制 iPad 进入纵向方向

发布于 2024-09-10 12:17:52 字数 1534 浏览 1 评论 0原文

我认为这里的问题是我正在尝试调用 mediaPicker 并且不支持其他方向...

有人可以解决这个问题吗?

这是我当前的代码:

- (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;
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凹づ凸ル 2024-09-17 12:17:52

这段代码过于复杂。首先,以模态方式呈现媒体选择器,然后将其呈现为弹出窗口;为什么?在弹出窗口中,您可以在呈现之前将其填充到导航控制器中;为什么?在 iPad 上呈现媒体选择器比这简单得多:

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];

它可以在任何方向上工作,甚至在弹出窗口显示时可以旋转。

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.

痴意少年 2024-09-17 12:17:52

所有预定义的模态控制器都支持所有方向,但它们必须从根视图控制器呈现,才能在方向和旋转方面正确运行。我的猜测是,代码中的“自我”不是根视图控制器。如果可能的话,您可能需要稍微重新构建代码才能实现这一点。

我还见过其他一些技巧,可以使其在不从根视图控制器呈现的情况下工作,但它们似乎都在自找麻烦,例如使用类别扩展 UIViewController 来覆盖 interfaceOrientation。

如果您可以从根视图控制器呈现它,这将是最简单和最干净的,但我意识到它并不总是可能的(例如,它位于您提供给第三方应用程序嵌入的库内)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文