【发布时间】:2014-10-22 11:54:58
【问题描述】:
我已经阅读了UIPopoverController docs 和超过 10 个 stackoverflow 解决方案,这些解决方案在 iOS 8.0.2 ipad 设备中均无效。
目标:
我想展示一个共享视图,当在外部单击时会关闭。
问题:
代码在 iOS 8.0 下运行良好。
代码(尝试了很多版本...):
NSArray *dataToShare = @[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.modalInPopover = YES;
self.act = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.act presentPopoverFromRect:CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, self.view.bounds.size.width/2, self.view.bounds.size.width/2) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
self.act.delegate = self;
我已经添加了:
UIPopoverControllerDelegate
我已经尝试过:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
尽快
UIPopoverController
显示视图失去焦点并且不注册触摸。因此,我无法处理 self.act rect 空间之外的触摸以强制解雇。
还有……
NSArray *dataToShare = @[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.modalInPopover = YES;
UIPopoverPresentationController *presentationController = [activityViewController popoverPresentationController];
presentationController.sourceView = self.view; // if button or change to self.view.
[self.parentViewController presentViewController:activityViewController animated:YES completion:nil];
我尝试从父母那里展示 UIActivityViewController 和 UIPopoverController,但也没有成功。
请帮忙,我在这个适用于 iOS 7.1 而不适用于 iOS 8+ 的愚蠢的东西上浪费了半天时间,这很荒谬,我仍然不知道为什么。
我会尝试添加在iOS8+中也消失的取消按钮。
更新1:
在 UIBarButtonItem 中添加 UIButton 并没有帮助...
UIButton *control = (UIButton *) sender;
[control setFrame:CGRectMake(300, 300, 100, 100)];
[control setBackgroundColor:[UIColor redColor]];
UIBarButtonItem *barButtonItemView = [[UIBarButtonItem alloc] init];
[barButtonItemView setCustomView:control];
[self.popup presentPopoverFromBarButtonItem:barButtonItemView permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
更新 2 - 2014 年 12 月 19 日。 (iOS 7.1. - 8.1.2):
接受的答案适用于 iOS 8.1。我在使用 iOS 7.1 时遇到了一些错误。所以我修改它以适用于 iOS 7.1。我已经在答案下方的 cmets 中说明了代码适用于哪些设备。
// publish - sharing
NSArray *dataToShare = @[url]; // ...or whatever pieces of data you want to share.
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
NSLog(@" SHARING - 1 - ");
[self presentViewController:activityViewController animated:YES completion:^{}];
}
else
{
// Change Rect to position Popover
NSLog(@" SHARING - 2 - ");
self.popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width * 3 / 5 ,
self.view.frame.size.width/2,
self.view.frame.size.width/10,
self.view.frame.size.width/10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
【问题讨论】:
标签: objective-c ios8 xcode6 uipopovercontroller uiactivityviewcontroller