iPhone 上的 UIPopoverPresentationController 和 iOS 10

UIPopoverPresentationController on iPhone with iOS 10

我正在尝试将 ViewController 作为弹出窗口显示在 iPhone 上。我已经通过 SO 和网络其他部分的几个答案,但 none 到目前为止已经工作。我写了一个简单的应用程序来测试这个。

ViewController.swift:

import UIKit

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clicked(_:)))
    }

    func clicked(_ sender: Any) {
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.blue
        vc.preferredContentSize = CGSize(width: 200, height: 200)
        vc.modalPresentationStyle = .popover
        present(vc, animated: true, completion: nil)

        let ppc = vc.popoverPresentationController
        ppc?.permittedArrowDirections = .any
        ppc?.delegate = self
        ppc?.barButtonItem = navigationItem.rightBarButtonItem
    }

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

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
}

故事板有一个空的 ViewController 嵌入在 NavigationController 中。

运行 这个,我希望弹出视图控制器显示在 "done" 按钮下。相反,蓝色视图控制器全屏显示。

有没有办法改变这种行为?

您正在呈现视图后连接委托。它将如何 return .none 来自委托并显示为弹出窗口。使用这个:-

    func clicked(_ sender: Any) {

        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.blue
        vc.modalPresentationStyle = .popover

        vc.preferredContentSize = CGSize(width: 200, height: 200)

        let ppc = vc.popoverPresentationController
        ppc?.permittedArrowDirections = .any
        ppc?.delegate = self
        ppc?.barButtonItem = navigationItem.rightBarButtonItem
        ppc?.sourceView = sender

        present(vc, animated: true, completion: nil)

    }

添加:

vc.popoverPresentationController?.delegate = self 

行前:

present(vc, animated: true, completion: nil)

import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {

override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(clicked(_:)))
    }



func clicked(_ sender: Any) {
        let vc = UIViewController()
        vc.view.backgroundColor = UIColor.blue
        vc.preferredContentSize = CGSize(width: 200, height: 200)
        vc.modalPresentationStyle = .popover
        let ppc = vc.popoverPresentationController
        ppc?.permittedArrowDirections = .any
        ppc?.delegate = self
        ppc!.sourceView = sender as? UIView 
        ppc?.barButtonItem = navigationItem.rightBarButtonItem
        present(vc, animated: true, completion: nil)
    }

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


}

上述解决方案在最近的 iOS 版本 12 及更高版本中不再有效。要让它再次工作,请覆盖 viewController 中的 -modalPresentationStyle 以显示为弹出窗口和 return UIModalPresentationPopover。 另外提供一个 popoverPresentationController.delegate,实现 adaptivePresentationStyleForPresentationController:traitCollection: 和 return UIModalPresentationNone.

@interface PopoverViewController : UIViewController
@end

@implementation PopoverViewController

- (UIModalPresentationStyle)modalPresentationStyle
{
    return UIModalPresentationPopover;
}

@end

@interface ViewController ()<UIPopoverPresentationControllerDelegate>
@end

@implementation ViewController

- (IBAction)openPopover:(id)sender
{
    UIViewController* testVC = [[PopoverViewController alloc] init];
    testVC.view.backgroundColor = UIColor.yellowColor;
    UIPopoverPresentationController* popPresenter = [testVC popoverPresentationController];
    popPresenter.permittedArrowDirections = UIPopoverArrowDirectionUp;
    popPresenter.delegate = self;
    popPresenter.sourceView = sender;
    popPresenter.sourceRect = [sender bounds];
    [self presentViewController:testVC animated:YES completion:^{}];
}

#pragma mark protocol (UIPopoverPresentationControllerDelegate)

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

@end