UIPopoverPresentationController显示全屏模态弹出窗口

3

我正在尝试将一个视图控制器显示为UIPopoverPresentationController,位于按钮下方或窗口中心。但它总是显示为全屏模态弹出。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MySecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"Pop"];

    // present the controller
    // on iPad, this will be a Popover
    // on iPhone, this will be an action sheet
    controller.modalPresentationStyle = UINavigationControllerOperationPop;
    [self presentViewController:controller animated:YES completion:nil];
    controller.preferredContentSize = CGSizeMake(280, 230);
    // configure the Popover presentation controller
    UIPopoverPresentationController *popController = [controller popoverPresentationController];
    popController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popController.delegate = self;

    // in case we don't have a bar button as reference
    popController.sourceView = self.showPop;
    popController.sourceRect = CGRectMake(384, -120, 280, 230);


-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}
4个回答

4

尝试这段代码,它是有效的

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SecondViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"pop"];

controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = CGSizeMake(280, 230);
// configure the Popover presentation controller

controller.popoverPresentationController.delegate = self;
controller.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;


// in case we don't have a bar button as reference
controller.popoverPresentationController.sourceView = self.view;
controller.popoverPresentationController.sourceRect = CGRectMake(384, -120, 280, 230);
//    controller.presentationController.delegate = self;
[self presentViewController:controller animated:YES completion:nil];

我要使用 bar button... 它会在全屏上显示 tablewiew... 我该怎么办...@Hassan Aftab - Suraj Sukale
1
@SurajSukale,也许对你来说已经太晚了,但或许其他人可以从这个评论中受益。你应该将一个代理分配给controller.popoverPresentationController,并在代理中实现方法adaptivePresentationStyle(for: UIPresentationController),返回值为UIModalPresentationStyleNone - Lukas1

2
我已经发布了另一个与同一问题相关的问题,并解决了我的问题。以下是问题的链接:UIPopoverPresentationController在iPhone上显示全屏模态 在ViewController.h中,首先创建一个UIPopoverPresenatationController属性。
@property(nonatomic,retain)UIPopoverPresentationController *dateTimePopover8;

然后展示 PopOverPresentationcontroller。
    UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:dateVC];
/*Here dateVC is controller you want to show in popover*/
                dateVC.preferredContentSize = CGSizeMake(280,200);
                destNav.modalPresentationStyle = UIModalPresentationPopover;
                _dateTimePopover8 = destNav.popoverPresentationController;
                _dateTimePopover8.delegate = self;
                _dateTimePopover8.sourceView = self.view;
                _dateTimePopover8.sourceRect = [sender frame];
                destNav.modalPresentationStyle = UIModalPresentationPopover;
                destNav.navigationBarHidden = YES;
                [self presentViewController:destNav animated:YES completion:nil];

您可能已经注意到,我们正在展示视图控制器而不是展示弹出窗口。因此,我们也需要用新的方式隐藏它。当我们点击屏幕时,它会自动隐藏。

-(void)hideIOS8PopOver
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

我们需要在实现文件中实现UIPopoverPresentationController的代理。请在实现文件中编写以下代理方法。
- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
    return UIModalPresentationNone;
}

0
在Storyboard中,这非常容易。只需从将触发操作的控件(例如UIBarButton或普通按钮)进行控制拖动到故事板视图控制器(如果是导航控制器的根视图,则拖动到导航控制器)。 选择segue并在属性检查器中更改Kind为“Present Modally”,presentation:Form sheet(如果您希望在中心显示它),选择所需的转换类型(默认值为cool)。

Attribute Inspector screenshot


0
如上所述,正如 @Lukas1 所提到的,您应该遵循 UIPopoverPresentationControllerDelegate 并实现 adaptivePresentationStyle 方法(实际上是定义在 UIAdaptivePresentationControllerDelegate 协议中的,UIPopoverPresentationControllerDelegate 的父类),并返回 .none。 以下是 Swift5 的实现代码:
extension ViewController: UIPopoverPresentationControllerDelegate {
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        return UIModalPresentationStyle.none
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接