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

UIPopoverPresentationController showing full screen modal popup always

我正在尝试将视图控制器显示为按钮下方或 window 中心的 UIPopoverPresentationController。但它总是显示为完整的 window 模式弹出窗口。

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

试试这个有效的代码

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

我针对同一问题发布了另一个问题,我已经解决了我的问题。这是问题的link:

在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.So 我们必须以新的方式隐藏它 also.It 当我们点击屏幕时自动隐藏。

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

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

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

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

正如@Lukas1 上面提到的,你应该遵循 UIPopoverPresentationControllerDelegate 并实现 adaptivePresentationStyle 方法(实际上在 UIAdaptivePresentationControllerDelegate 协议中定义,UIPopoverPresentationControllerDelegate parent class)和 return .none 这是 Swift5 中的实现:

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