iOS 7 custom UIActivity as popover

喜你入骨 提交于 2019-12-22 12:43:39

问题



This is my first question on StackOverflow, so please be patient with me...

Here's the problem:
I have created a custom Activity for adding bookmarks, which is opened from the UIActivityViewController. On iPhone it is opened as a modal, which is ok. But on iPad I open the UIActivity in a Popover, and I want the custom BookmarkActivity also to be opened in this Popover.
The method

-(UIViewController *)activityViewController in my UIActivity subclass looks like this:

- (UIViewController *)activityViewController {

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        GlobalBookmarksAddViewController *bookmarkViewController = [self.parentController.storyboard instantiateViewControllerWithIdentifier:@"GlobalBookmarksAddViewController"];

        bookmarkViewController.openedDocument = self.openedDocument;
        bookmarkViewController.yAxis = self.yAxis;
        bookmarkViewController.parentActivity = self;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bookmarkViewController];

        nav.modalInPopover = YES;
        //nav.modalPresentationStyle = UIModalPresentationFormSheet;
        nav.modalPresentationStyle = UIModalPresentationCurrentContext;

        return nav;
    } else {
        GlobalBookmarksAddViewController *bookmarkViewController = [self.parentController.storyboard instantiateViewControllerWithIdentifier:@"GlobalBookmarksAddViewController"];

        bookmarkViewController.openedDocument = self.openedDocument;
        bookmarkViewController.yAxis = self.yAxis;
        bookmarkViewController.parentActivity = self;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bookmarkViewController];

        return nav;
    }
}

With this code, the bookmark controller is shown in the popover for about 0.2 sec and then the popover disappears. If I change the property nav.modalPresentationStyle from UIModalPresentationCurrentContext to UIModalPresentationFormSheet the bookmarkController is correctly shown, but in a normal popup.

I did some research already and I have found two links:
UIActivity activityViewController being presented modally on iPad instead of in popover
-[UIActivity activityViewController] is not presented in a UIPopoverController

Both of them suggest, that it's not possible at the moment, but somehow it must be possible - it works in Safari (also the "Add Bookmark" feature).

So the questions are:

  1. How to change this behavior of the custom view controller to be opened in popover correctly?
  2. If the only possibility at the moment is to open it as a normal popup, is there a way to resize it? I have tried changing the navigation controllers and the bookmarks controllers size and frame, but it's not working.

Edit: I have contacted Apple about this, and here's the solution:
The method -(UIViewContreoller *)activityController looks now like this:

-(UIViewController *)activityViewController
{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        return nil;
    }
    else
    {
        GlobalBookmarksAddViewController *bookmarkViewController = [self.parentController.storyboard instantiateViewControllerWithIdentifier:@"GlobalBookmarksAddViewController"];

        bookmarkViewController.openedDocument = self.openedDocument;
        bookmarkViewController.yAxis = self.yAxis;

        bookmarkViewController.parentActivity = self;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bookmarkViewController];

        return nav;
    }
}

And the code for the view in popover has been moved to the -(void)performActivity method:

-(void)performActivity
{
     if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
    {
        GlobalBookmarksAddViewController *bookmarkViewController = [self.parentController.storyboard instantiateViewControllerWithIdentifier:@"GlobalBookmarksAddViewController"];

        bookmarkViewController.openedDocument = self.openedDocument;
        bookmarkViewController.yAxis = self.yAxis;

        bookmarkViewController.parentActivity = self;

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bookmarkViewController];

        nav.modalInPopover = YES;
        nav.modalPresentationStyle = UIModalPresentationCurrentContext;

        // self.activityController is my own property set for the activity after the UIActivityViewController is created
        [self.activityController presentViewController:nav animated:YES completion:NULL];
    }
}

来源:https://stackoverflow.com/questions/23611070/ios-7-custom-uiactivity-as-popover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!