装有iOS 10的iPhone上的UIPopoverPresentationController [英] UIPopoverPresentationController on iPhone with iOS 10
问题描述
我正在尝试在iPhone上显示ViewController作为弹出框.我已经在SO和Web的其余部分上找到了几个答案,但到目前为止,都没有一个有效的方法.我写了一个简单的应用程序对此进行测试.
I'm trying to display a ViewController as a popover on an iPhone. I have already been through several answers on SO and the rest of the web but none have worked so far. I wrote a simple app to test this.
ViewController.swift:
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
}
}
故事板在NavigationController中嵌入了一个空的ViewController.
The storyboard has an empty ViewController embedded in a NavigationController.
运行此命令,我希望弹出窗口视图控制器显示在完成"按钮下.取而代之的是,全屏显示蓝色视图控制器.
Running this, I expected a popover view controller to show under the "done" button. Instead, the blue view controller is presented full screen.
有没有办法改变这种行为?
Is there a way to change this behaviour?
推荐答案
显示视图后,您正在连接委托.它如何从委托中返回.none并显示为弹出窗口.使用这个:-
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)
}
这篇关于装有iOS 10的iPhone上的UIPopoverPresentationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!