UIPopoverPresentationController 始终显示全屏模式弹出窗口

Posted

技术标签:

【中文标题】UIPopoverPresentationController 始终显示全屏模式弹出窗口【英文标题】:UIPopoverPresentationController showing full screen modal popup always 【发布时间】:2016-03-28 05:07:13 【问题描述】:

我试图将视图控制器显示为按钮下方或窗口中心的 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;

【问题讨论】:

【参考方案1】:

试试这段代码吧

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];

【讨论】:

我将使用栏按钮...它在全屏上显示我的桌面...我应该怎么做...@Hassan Aftab @SurajSukale,对你来说可能为时已晚,但也许其他人可以从这条评论中受益。您应该为 controller.popoverPresentationController 分配一个委托,并在委托实现方法 adaptivePresentationStyle(for: UIPresentationController) 中,返回值 UIModalPresentationStyleNone【参考方案2】:

我针对同一个问题发布了另一个问题,并且我已经解决了我的问题。这是问题的链接: UIPopoverPresentationController is showing full screen modal on 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];

你一定注意到我们是在展示View Controller而不是展示popOver。所以我们也必须以新的方式隐藏它。当我们点击屏幕时它会自动隐藏。

-(void)hideios8PopOver

    [self dismissViewControllerAnimated:YES completion:nil];

我们必须在实现文件中实现 UIPopoverPresenatationController 的委托。在实现文件中写下面的委托方法。

- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller 
    return UIModalPresentationNone;

【讨论】:

【参考方案3】:

在 Storyboard 中,这很容易。只需控制从将触发操作的控件(例如 UIBarButton 或普通按钮)拖动到情节提要视图控制器(如果是导航控制器的根视图,则拖动到导航控制器)。 选择segue并将属性检查器中的Kind更改为“Present Modally”,presentation:Form sheet(如果您希望它显示在中心),选择您想要的过渡类型(默认为cool)。

【讨论】:

【参考方案4】:

正如上面提到的@Lukas1,你应该符合UIPopoverPresentationControllerDelegate并实现adaptivePresentationStyle方法(实际上定义在UIAdaptivePresentationControllerDelegate协议,UIPopoverPresentationControllerDelegate父类中)并返回.none 这是 Swift5 中的实现:

extension ViewController: UIPopoverPresentationControllerDelegate 
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle 
        return UIModalPresentationStyle.none
    


【讨论】:

以上是关于UIPopoverPresentationController 始终显示全屏模式弹出窗口的主要内容,如果未能解决你的问题,请参考以下文章