【问题标题】:iOS, PopOverPresentationController issue, .xibiOS,PopOverPresentationController 问题,.xib
【发布时间】:2015-11-14 21:18:28
【问题描述】:

我有一个 NavigationController,另一个控制器被压入它的堆栈:BNRDetailsViewController。现在,在 BNRDetailsViewController 中,我试图在按下工具栏按钮时显示一个弹出窗口,这将向我展示 UIImagePickerController(仅在 iPad 设备上)。因此,我尝试遵循以下 stackoverflow 线程:

UIPopoverPresentationController on iOS 8 iPhone

但没有成功。如果我只使用他们的代码,我会收到一条错误消息,即pushing navigationController is not supported。如果我尝试像这样推送新创建的UIPopoverPresentationController[self.navigationController pushViewController:self.imagePickerPopover animated:YES]; 它会崩溃,因为UIPopoverPresentationController 不是UIViewController 类型,所以,我想,我不能只是将它推送到堆栈上。

在这种特殊情况下,您建议怎么做?

这是我现在拥有的在按下工具栏按钮时触发的代码:

- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

// If the device have camera, take a picture, otherwise,
// just pick from photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

imagePicker.delegate = self;

// Place image picker on the screen
//[self presentViewController:imagePicker animated:YES completion:NULL];

// Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // Create a new popover controller that will display the imagePicker
    _imagePickerPopover = [[UIPopoverPresentationController alloc] initWithPresentedViewController:imagePicker presentingViewController:self];

    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    _imagePickerPopover.delegate = self;
    _imagePickerPopover.sourceView = self.view;
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;
    _imagePickerPopover.sourceRect = frame;
   // [self.navigationController pushViewController:self.imagePickerPopover animated:YES];
}
}

【问题讨论】:

  • 基本上你想在 iOS 8+ 上呈现一个弹出框。请参阅此问题中的示例代码:stackoverflow.com/q/28521583/558933。该代码在 iPhone 和 iPad 上都显示了一个弹出框。如果您希望弹出框仅出现在 iPad 而不是 iPhone 上,则返回适当的 UIModalPresentationStyle 值。
  • 谢谢!但是,我不使用情节提要,而是使用 .xib 文件。所以,我仍然不确定在这种情况下该怎么做。

标签: ios objective-c iphone ipad nib


【解决方案1】:

这是基于您的代码的代码,它将使弹出框显示在 iPad 上。

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    imagePicker.modalPresentationStyle = UIModalPresentationPopover;
    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;

    UIPopoverPresentationController *popoverController = 
        imagePicker.popoverPresentationController;
    popoverController.sourceView = self.view;
    popoverController.sourceRect = frame;

    [self.navigationController presentViewController:imagePicker 
                               animated:YES completion:nil];
}

【讨论】:

  • 好的,我明白了。只需更改一行:popoverController.sourceView = self.viewpopoverController.sourceView = self.toolbar 其中toolbar 被定义为 BNRDetailsViewController 类中的属性:@property (weak, nonatomic) IBOutlet UIToolbar *toolbar
  • 请注意:preferredContSizre 替换为 popoverContentSize
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2011-10-23
  • 2011-07-22
  • 1970-01-01
  • 2015-04-29
相关资源
最近更新 更多