如何在iOS 8+中以编程方式配置和呈现iPhone上的弹出窗口

时间:2015-07-06 00:33:46

标签: ios objective-c

如何使用Objective-C代码,没有故事板或其他Interface Builder工件,在iOS 8中的弹出窗口(所有尺寸和方向)上呈现UIViewController。

1 个答案:

答案 0 :(得分:19)

在iOS 8中,您可以将任何视图控制器配置为弹出窗口,如下所示:

UIViewController* controller = ...; // your initialization goes here

// set modal presentation style to popover on your view controller
// must be done before you reference controller.popoverPresentationController
controller.modalPresentationStyle = UIModalPresentationPopover;
controller.preferredContentSize = CGSizeMake(150, 300);

// configure popover style & delegate
UIPopoverPresentationController *popover =  controller.popoverPresentationController;
popover.delegate = controller;
popover.sourceView = sourceView;
popover.sourceRect = CGRectMake(150,300,1,1);
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;

// display the controller in the usual way
[self presentViewController:controller animated:YES completion:nil];

因此它在iPhone上显示为弹出框,将其添加到弹出框的UIPopoverPresentationControllerDelegate代表(您在上面设置):

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