UIPopoverPresentationController on iPhone with iOS 10

女生的网名这么多〃 提交于 2019-12-01 09:58:20

You are connecting delegate after presenting view. How it will return .none from delegate and show as popover. Use this :-

    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)

    }
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
    }


}

The solutions above no longer work in recent iOS versions 12 and above. To get it working again, override -modalPresentationStyle within the viewController to be presented as a popover and return UIModalPresentationPopover. Additionally provide a popoverPresentationController.delegate, implement adaptivePresentationStyleForPresentationController:traitCollection: and 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

Add:

vc.popoverPresentationController?.delegate = self 

just before the line:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!