我有这个share
按钮,从屏幕底部显示共享菜单。
这是我使用的代码:
-(IBAction)ShareButton:(id)sender {
NSString *texttoshare = @"My Text";
UIImage *imagetoshare = _MyImage;
NSArray *activityItems = @[texttoshare, imagetoshare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
它适用于大量iPhone,但在iPad上崩溃。
如何为iPad实施正确的ViewControllers?
我猜这里可以添加一些内容:
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//add alternative for iPad
}
谢谢。
答案 0 :(得分:0)
做以下事情:
将此代码放入viewDidLoad
:
UIBarButtonItem *shareButton;
shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share:)];
self.navigationItem.rightBarButtonItem = shareButton;
然后创建它的动作。
-(void)share:(id)sender {
UIImage *image = [UIImage imageWithData:[chart getImage]];
NSArray *activityItems = @[image];
UIActivityViewController *activityViewControntroller = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewControntroller.excludedActivityTypes = @[];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
activityViewControntroller.popoverPresentationController.sourceView = self.view;
activityViewControntroller.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/4, 0, 0);
}
[self presentViewController:activityViewControntroller animated:true completion:nil];
}
希望它会起作用..