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

时间:2016-04-05 06:33:45

标签: ios swift uiviewcontroller popover

一般来说,我按照此处的说明操作:https://stackoverflow.com/a/24687152/3741933

但是,正如其评论中所讨论的那样,无论preferredContentSize还是sourceRect,popover始终是全屏的。

显示弹出窗口的按钮:

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

UIViewController:

import UIKit

class EmptyViewController : UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.redColor()
    }
}

enter image description here

  

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

4 个答案:

答案 0 :(得分:6)

有可能!我不知道从那时起......

在您显示为弹出框的viewDidLoad的{​​{1}}中:

UIViewController

在显示弹出窗口的 self.preferredContentSize = CGSize(width: myWidth, height: myHeight) 中,将类设置为UIViewController并且:

UIPopoverPresentationControllerDelegate

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

就是这样!

答案 1 :(得分:3)

对于那些正在寻找无须做事的人?

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

iPhone 11 Pro Max上的结果

enter image description here

答案 2 :(得分:0)

使用此代码,您无法在带有纵向模式的iPhone中执行此操作...您可以查看popover section in apple doc here..

它表明:

  

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

正如我所说,如果您可以在iPad上办理登机手续,您的内容可以显示在popover内。

答案 3 :(得分:0)

您只需要实现sceneName协议的下一个方法:

UIPopoverPresentationControllerDelegate