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

时间:2016-03-28 05:07:13

标签: ios objective-c iphone uipopovercontroller uipopover

我正在尝试将视图控制器显示为按钮下方或窗口中心的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 个答案:

答案 0 :(得分:3)

尝试使用此代码

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

答案 1 :(得分: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.So我们也必须以新的方式隐藏它。当我们点击屏幕时它会自动隐藏。

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

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

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

答案 2 :(得分:0)

在Storyboard中,这非常简单。只需控制从触发操作的控件(例如UIBarButton或普通按钮)到故事板视图控制器的拖动(如果导航控制器的根视图,则拖动到导航控制器)。 选择segue并将属性检查器中的Kind更改为“Present Modately”,演示文稿:Form sheet(如果您希望它显示在中心),选择您想要的过渡类型(默认为酷)。

Attribute Inspector screenshot

答案 3 :(得分:0)

如上述@ Lukas1所示,您应该遵循UIPopoverPresentationControllerDelegate并实现adaptivePresentationStyle方法(实际上是在UIAdaptivePresentationControllerDelegate协议,UIPopoverPresentationControllerDelegate父类中定义)并返回.none 这是Swift5中的实现:

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