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

Wil*_*iss 6 objective-c ios

你怎么能在iPhone 8(所有大小和方向)的popover中呈现UIViewController,在iOS 8中只使用Objective-C代码,没有Story Boards或其他Interface Builder工件.

Wil*_*iss 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];
Run Code Online (Sandbox Code Playgroud)

因此它在iPhone上显示为弹出窗口,将其添加到UIPopoverPresentationControllerDelegatepopover 的委托(您在上面设置):

- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller
{
    return UIModalPresentationNone;
}
Run Code Online (Sandbox Code Playgroud)