如何在iPhone上以编程方式在Swift中呈现UIViewController作为弹出框

Ziz*_* Wu 2 uiviewcontroller popover ios swift

一般我按照这里的说明:https://stackoverflow.com/a/24687152/3741933.

然而,在其评论中讨论的,酥料饼的始终,无论全屏preferredContentSizesourceRect.

显示弹出窗口的按钮:

func buttonClicked(sender: UIButton!) {
    let ac = EmptyViewController() as UIViewController
    ac.modalPresentationStyle = .Popover
    ac.preferredContentSize = CGSizeMake(200, 200)
    let popover = ac.popoverPresentationController
    popover?.delegate = self
    popover?.permittedArrowDirections = .Any
    popover?.sourceView = self.view
    popover?.sourceRect = CGRect(x: 100, y: 100, width: 100, height: 100)

    presentViewController(ac, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)

UIViewController:

import UIKit

class EmptyViewController : UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.redColor()
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想知道如何使它成为一个真正的popover(不是全屏大小).顺便说一下,正如@EI Captain所说,它在iPad上完美运行,但在iPhone上总是全屏.

lor*_*nzo 6

有可能的!虽然我不确定...

viewDidLoad你的UIViewController那个显示为弹出窗口:

    self.preferredContentSize = CGSize(width: myWidth, height: myHeight)
Run Code Online (Sandbox Code Playgroud)

UIViewController显示弹出窗口时,将类设置为UIPopoverPresentationControllerDelegate和:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "Identifier" {
        let vc: UIViewController = segue.destination

        let pvc = vc.popoverPresentationController
        pvc?.delegate = self
        return
    }

}

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none
}
Run Code Online (Sandbox Code Playgroud)

PS:在故事板中,不要忘记将segue类设置为"Present as Popover"

而已!

  • 没有segue怎么办? (3认同)

S.B*_*oda 6

对于那些正在寻找如何在没有 segue 的情况下进行的人

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .systemBackground
        
        let button = UIButton()
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setImage(UIImage(systemName: "square.grid.2x2.fill"), for: .normal)
        button.addTarget(self, action: #selector(displayPopover), for: .touchUpInside)
        self.view.addSubview(button)
        
        NSLayoutConstraint.activate([
            button.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100),
            button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            button.widthAnchor.constraint(equalToConstant: 40),
            button.heightAnchor.constraint(equalToConstant: 40),
        ])
        
    }
    
    @IBAction func displayPopover(sender: UIButton!) {
        let popoverVC = PopoverViewController()
        popoverVC.modalPresentationStyle = .popover
        popoverVC.popoverPresentationController?.sourceView = sender
        popoverVC.popoverPresentationController?.permittedArrowDirections = .up
        popoverVC.popoverPresentationController?.delegate = self
        self.present(popoverVC, animated: true, completion: nil)
    }
}

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

class PopoverViewController: UIViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = .systemGray
        self.preferredContentSize = CGSize(width: 300, height: 200)
    }
}
Run Code Online (Sandbox Code Playgroud)

结果于 iPhone 11 Pro Max

在此处输入图片说明


EI *_*2.0 0

您不能使用此代码在具有纵向模式的 iPhone 中执行此操作...您可以在此处查看苹果文档中的弹出框部分。

它建议:

在 iOS 8 及更高版本中,您可以使用 UIPopoverPresentationController 来呈现弹出框。UIPopoverPresentationController 定义了一个委托,可让您调整弹出窗口内容的显示样式以适应当前的显示环境。例如,在水平规则环境中,您的内容可以显示在弹出窗口内;在水平紧凑的环境中,您的内容可以以全屏模式视图显示。

正如我所说,如果您可以在 iPad 中查看,您的内容就可以显示在弹出窗口内。