ios - UIActivity activityViewController 以模态方式呈现在 iPad 上而不是弹出窗口中

标签 ios ipad sdk popover uiactivity

在 iOS 6 中使用客户 UIActivity 子类时,可以指定一个自定义 View Controller ,当您从初始 UIActionViewController 的 View 中选择您的操作时将显示该 View Controller 。您可以通过从 UIActivity 子类的 activityViewController 方法返回对自定义 View Controller 的引用来执行此操作。

根据UIActivity class reference :

activityViewController

The default implementation of this method returns nil. Subclasses that provide additional UI using a view controller can override this method to return that view controller. If this method returns a valid object, the system presents the returned view controller for you, instead of calling the performActivity method. On iPad, your view controller is presented inside of a popover. On iPhone and iPod touch, your view controller is presented modally.

Your custom view controller should provide a view with your custom UI and should handle any user interactions inside those views. Upon completing the activity, do not dismiss the view controller yourself. Instead, call the activityDidFinish: method and let the system dismiss it for you.

请注意第一段末尾的那一点:在 iPad 上,您的 View Controller 显示在弹出框内。在 iPhone 和 iPod touch 上,您的 View Controller 以模态方式呈现。

但是,在 iPad 上,activityViewController 返回的 View Controller 始终以模态显示,无论我如何呈现 UIActivityViewController(模态或通过弹出窗口)。当通过弹出窗口呈现时,它会导致崩溃,因为它认为它没有被关闭。

我做错了什么?这是 iOS 6 中的错误吗?


更新:这里有一个简单的 Xcode 项目可以说明问题。随意克隆它并尝试看看你是否能看出我们哪里出错了:github.com/simonwhitaker/GSActivityDemo

最佳答案

因为我们正在谈论 UIActivityViewController,它是向用户显示可用事件的 View 。 Apple 声明如下...

Your app is responsible for configuring, presenting, and dismissing this view controller. Configuration for the view controller involves specifying the data objects on which the view controller should act. (You can also specify the list of custom services your app supports.) When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.

我将最后一行作为您必须处理 View 呈现方式的标志,因此我检查代码是否在 iPad 上运行并相应地使用 UIPopover。正如您在这里看到的... https://github.com/bufferapp/buffer-uiactivity/blob/master/BufferUIActivity/Views/FirstViewController.m在以下方法中。

-(IBAction)openUIActivityView:(id)sender {

    NSString *text = @"Hello world";
    NSString *url = @"http://bufferapp.com";


    NSArray *activityItems = @[text, url];

    BufferUIActivity *bufferActivity = [[BufferUIActivity alloc] init];

    UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[ bufferActivity ]];


    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        [self presentViewController:activityView animated:YES completion:^{

        }];
    } else {
        // Change Rect to position Popover
        self.popup = [[UIPopoverController alloc] initWithContentViewController:activityView];
        [self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.width/2, 100, 100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }

}

关于ios - UIActivity activityViewController 以模态方式呈现在 iPad 上而不是弹出窗口中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433718/

相关文章:

ios - 如果状态栏不可见,检索当前界面方向是否可靠?

android - AVG 杀毒软件删除了 adb.exe

android - Android中图像的大小

android - Android SDK 中缺少 AAPT2

ios - 如何在 IOS 9 中为通用链接创建 apple-app-site-association 文件?

ios - UICollectionView 页脚

ipad - UIPopoverController 显示位置错误

ios - 如何在我的应用程序中启用 "Photos"的编辑功能? [ swift 3]

ios - 有没有办法动态创建当前类类型的实例?

iOS RxSwift 如何防止序列被处理(抛出错误)?