如何在没有故事板的情况下以编程方式在 Swift 中调整弹出窗口的大小?

How to size a popup programmatically in Swift without storyboard?

我正在尝试显示弹出视图,但无法正确调整它的大小。我有以下代码显示我的弹出窗口,但 preferredContentSize 不起作用。查看总是全屏显示,这不是我想要的。如何正确调整弹出窗口的大小?

func presentPopup() {
        let popupVC = MyPopupViewController()
        popupVC.modalPresentationStyle = .overCurrentContext
        popupVC.modalTransitionStyle = .crossDissolve
        popupVC.popoverPresentationController?.permittedArrowDirections = .any
        // the following does not size the view, always come up full screen
        popupVC.preferredContentSize = CGSize(width: 200, height: 200) 
        present(popupVC, animated: true, completion: nil)
}

我想实现类似下面的东西。

请注意:我不使用故事板,我正在寻找不使用或实例化故事板的解决方案。

添加以下代码以实例化您的演示控制器并设置所需的配置。

let popupVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyPopoverVC")
popupVC.modalPresentationStyle = .popover
popupVC.preferredContentSize = CGSize(width: 200, height: 200)

let presentationController = MyControllerPresentAsPopover.configurePresentation(forController: popupVC)
presentationController.sourceView = sender
presentationController.sourceRect = sender.bounds
presentationController.permittedArrowDirections = [.any]
present(popupVC, animated: true, completion: nil)

然后创建名为MyControllerPresentAsPopover的class,它实现了UIPopoverPresentationControllerDelegate的委托方法。请参阅下面的代码:

class MyControllerPresentAsPopover : NSObject, UIPopoverPresentationControllerDelegate {

    private static let sharedInstance = MyControllerPresentAsPopover()

    private override init() {
        super.init()
    }

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

    static func configurePresentation(forController controller : UIViewController) -> UIPopoverPresentationController {
        controller.modalPresentationStyle = .popover
        let presentationController = controller.presentationController as! UIPopoverPresentationController
        presentationController.delegate = MyControllerPresentAsPopover.sharedInstance
        return presentationController
    }
}

我找到了实现目标的更好方法。我没有在主视图中调整弹出窗口的大小,而是将调整大小的逻辑移到了 popUpView 本身。这是最终代码。

func presentPopup() {
        let popupVC = MyPopupViewController()
        popupVC.modalPresentationStyle = .overCurrentContext
        popupVC.modalTransitionStyle = .crossDissolve
        present(popupVC, animated: true, completion: nil)
}

MyPopupViewController

class MyPopupViewController: UIViewController {
    // UI elements for popup will be added to popupBox view
    let popupBox: UIView = {
       let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .green
        return view
    }()

override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .clear

        self.definesPresentationContext = true

        setupViews()

    }


func setupViews() { 
     view.addSubview(popupBox)

     // autolayout constraint for popupBox
        popupBox.heightAnchor.constraint(equalToConstant: 200).isActive = true
        popupBox.widthAnchor.constraint(equalToConstant: 300).isActive = true
        popupBox.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        popupBox.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

}