Objective c - UIActivityViewController orientation

2019-02-25 00:45发布

问题:

My iPhone app is design to support portrait orientation only, except one view controller that present photos, and support landscape mode as well.

So overall my project support all the orientations, but every view controller (except the photo view controller) implement the method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}  

The problem:

In one of my (only portrait) view controller, I'm using UIActivityViewController to let the user choose sharing content through email or SMS.

When the user pick the email option, it automatically shows the ios native email view controller.

If the user now change the device's orientation to landscape, the email view controller also changes to landscape, now if the user tap the cancel/send button, the email controller get dismissed, and now my all app is in landscape!

Is there a way to force the eamil view controller to be in portrait mode only?

I know I can create my own email view controller and subclass UIActivity to show it, but than the UIActivityViewController won't show the default email icon when presented, and I really like it to show it and not some other (must be grey) icon that I'll provide.

回答1:

You should make another uinavigationcontroller and present uiactivityviewcontroller from there. In this code _image is a UIImage yo wan to share. BlankViewController is just place holder viewcontroller you can create in IB you can also make it's view's background colour to clear and do what ever appearance changes you want to do.

__weak  CurrentViewController  *weakSelf    =   self  ;

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[_image] applicationActivities:nil]   ;
UIViewController        *viewC  =   [[UIViewController alloc] initWithNibName:@"BlankViewController" bundle:nil] ;
UINavigationController  *navC   =   [[UINavigationController alloc] initWithRootViewController:viewC]  ;
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed)
{
    [weakSelf dismissViewControllerAnimated:NO completion: ^ {
        ;    // Your completion handler
    }]  ;
};
[self presentViewController:navC animated:NO completion: ^ {
    [navC presentViewController:activityViewController animated:YES completion: ^ {
        ;
    }]  ;
}];


回答2:

i had similar problems in the app i am currently developing. i ended up overwriting more of the rotation methods to make sure my own viewcontroller stays in portrait.

that's what worked for me (IOS5+):

- (BOOL) shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation (UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

if you are pre ios5 or that's not working for you have a look at: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

hope you get it to work. :)



回答3:

May be u should try

- (void)viewWillAppear:(BOOL)animated
{
   [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
   [super viewWillAppear:animated];
}

on your view?



回答4:

Couldn't found a solution to this problem, so I ended up implementing my own email UIActivity subclass...