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