iOS 8 iPhone上的UIPopoverPresentationController

Dav*_*ave 74 iphone objective-c uipopovercontroller uipopover ios8

有谁知道是否UIPopoverPresentationController可以用来在iPhone上展示弹出窗口?想知道Apple是否在iOS 8上添加了此功能,试图为iPad和iPhone创建更统一的演示控制器.

不确定是否可以提出/回答Beta的问题.在那种情况下我会删除它.

Iva*_*hoo 83

您可以UIModalPresentationFullScreen使用adaptivePresentationStyleForPresentationController:可用的方法覆盖默认的自适应行为(在紧凑的水平环境中,即iPhone) UIPopoverPresentationController.delegate.

UIPresentationController使用此方法来询问要使用的新演示文稿样式,在您的情况下,只需返回UIModalPresentationNone将导致UIPopoverPresentationController呈现为弹出框而不是全屏.

这是一个使用故事板中的segue设置从一个UIBarButtonItem到" 现在模态 "a 的popover的例子UIViewController

class SomeViewController: UIViewController, UIPopoverPresentationControllerDelegate {

    // override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { // swift < 3.0
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "PopoverSegue" {
            if let controller = segue.destinationViewController as? UIViewController {
                controller.popoverPresentationController.delegate = self
                controller.preferredContentSize = CGSize(width: 320, height: 186)                
            }
        }
    }

    // MARK: UIPopoverPresentationControllerDelegate

    //func adaptivePresentationStyleForPresentationController(controller: UIPresentationController!) -> UIModalPresentationStyle { // swift < 3.0
    func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
        // Return no adaptive presentation style, use default presentation behaviour
        return .None
    }
}
Run Code Online (Sandbox Code Playgroud)

WWDC 2014会议214"在iOS8中查看控制器进展"(36:30)中提到了这个技巧

  • 解决方案是在调用presentViewController之前设置popover.这与Apple的示例正好相反,他们明确告诉您在调用presentViewController后设置popover. (17认同)
  • 对于那些无法阅读Swift的人来说,obj-c中的相同代码:http://stackoverflow.com/a/28143620/1228075 (4认同)
  • 这在GM for iPhone中被窃听.如果您尝试在模拟器处于纵向状态时显示,则它始终为全屏.如果您旋转到横向,它将变为弹出窗口.如果再次旋转回肖像,它将保持弹出状态. (2认同)

Des*_*ose 73

如果有人想要仅使用代码提供popover,您可以使用以下方法.

目标 - C.

声明属性UIPopoverPresentationController:

@property(nonatomic,retain)UIPopoverPresentationController *dateTimePopover8;
Run Code Online (Sandbox Code Playgroud)

使用以下方法显示UIButton的弹出窗口:

- (IBAction)btnSelectDatePressed:(id)sender
{
    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.navigationBarHidden = YES;
    [self presentViewController:destNav animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

使用以下方法从UIBarButtonItem呈现弹出窗口:

- (IBAction)btnSelectDatePressed:(id)sender
{
    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;
     CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y+20;
    _dateTimePopover8.sourceRect = frame;
    destNav.navigationBarHidden = YES;
    [self presentViewController:destNav animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

在视图控制器中也实现此委托方法:

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

要关闭此弹出窗口,只需关闭视图控制器即可.下面是关闭视图控制器的代码:

-(void)hideIOS8PopOver
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

迅速

使用以下方法显示UIButon的popover:

func filterBooks(sender: UIButon)
    {
        let filterVC =  FilterDistanceViewController(nibName: "FilterDistanceViewController", bundle: nil)
        var filterDistanceViewController = UINavigationController(rootViewController: filterVC)
        filterDistanceViewController.preferredContentSize = CGSizeMake(300, 205)
        let popoverPresentationViewController = filterDistanceViewController.popoverPresentationController
        popoverPresentationViewController?.permittedArrowDirections = .Any
        popoverPresentationViewController?.delegate = self
        popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
        popoverPresentationViewController!.sourceView = self.view;
        popoverPresentationViewController!.sourceRect = sender.frame

        filterDistanceViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
        filterDistanceViewController.navigationBarHidden = true
        self.presentViewController(filterDistanceViewController, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)

使用以下方法从UIBarButtonItem呈现弹出窗口:

func filterBooks(sender: UIBarButtonItem)
    {
        let filterVC =  FilterDistanceViewController(nibName: "FilterDistanceViewController", bundle: nil)
        var filterDistanceViewController = UINavigationController(rootViewController: filterVC)
        filterDistanceViewController.preferredContentSize = CGSizeMake(300, 205)
        let popoverPresentationViewController = filterDistanceViewController.popoverPresentationController
        popoverPresentationViewController?.permittedArrowDirections = .Any
        popoverPresentationViewController?.delegate = self
        popoverPresentationController?.barButtonItem = self.navigationItem.rightBarButtonItem
        popoverPresentationViewController!.sourceView = self.view;
        var frame:CGRect = sender.valueForKey("view")!.frame
        frame.origin.y = frame.origin.y+20
        popoverPresentationViewController!.sourceRect = frame

        filterDistanceViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
        filterDistanceViewController.navigationBarHidden = true
        self.presentViewController(filterDistanceViewController, animated: true, completion: nil)
    }
Run Code Online (Sandbox Code Playgroud)

在视图控制器中也实现此委托方法:

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle{
        return .None
    }
Run Code Online (Sandbox Code Playgroud)

请确保UIPopoverPresentationControllerDelegate在.h/.m/.swift文件中添加委托


aga*_*ndi 10

问题: iPhone弹出窗口全屏显示,不尊重preferredContentSize值.

解决方案:与Apple在UIPopoverPresentationController类引用中建议的内容相反,在获取对popover表示控制器的引用并对其进行配置呈现视图控制器.

// Get the popover presentation controller and configure it.
//...

// Present the view controller using the popover style.
[self presentViewController:myPopoverViewController animated: YES completion: nil]; 
Run Code Online (Sandbox Code Playgroud)