OStack程序员社区-中国程序员成长平台

标题: iOS,PopOverPresentationController 问题,.xib [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:09
标题: iOS,PopOverPresentationController 问题,.xib

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

UIPopoverPresentationController on iOS 8 iPhone

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

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

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

- (IBAction)takePictureid)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];
}
}



Best Answer-推荐答案


这是基于您的代码的代码,它将使弹出框显示在 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];
}

关于iOS,PopOverPresentationController 问题,.xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33713482/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4