How to center NSPopover when using SwiftUI?

Viewed 476

If the NSPopover has its contentViewController set to some NSViewController that does not use SwiftUI directly on its view. For instance

final class ViewController: NSViewController {
    private lazy var contentView = NSView()

    override func loadView() {
       view = contentView
    }

    init() {
       super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
       fatalError()
    }
}

let controller = ViewController()
popover.contentViewController = controller

It will be displayed centered.

However, if we simply change the line of the corresponding view:

private lazy var contentView = NSHostingView(rootView: Blah())

Where Blah is

struct Blah: View {
    var body: some View {
        ZStack {
            Text("blah")
        }
        .frame(width: 400, height: 600, alignment: .center)
        .background(Color.green)
    }
}

You can see that the view is not centralized anymore. So, how can we let the NSViewController centralized in relation to the NSStatusItem item? (In the images those are one check icon)

enter image description here

If you give a close look at the images, you can see that the image with the white view has the popover arrow in the middle, while the other has not.

enter image description here

0 Answers
Related