How to change NSPopover background color include triangle part?
Unfortunately, this isn't really possible. NSPopover wasn't designed to be customizable. If you want a completely customized appearance, your best bet is probably to use a third-party, open-source NSPopover replacement like INPopoverController.
How to position NSPopover in status bar application (macOS)
In your code just add a "random" larger size than the popover itself.
I think this happens because the size of the popover is not calculated right away so there is a race condition in there, but this seems to work pretty well for me /p>
popOver.contentSize = NSSize(width: 600, height: 1)
How do I make a Menu Bar (NSPopover) App active when clicking on the menu button?
I fixed it myself in the end, it turned out to be a very simple fix as expected.
All I needed to to was call NSApplication.sharedApplication().activateIgnoringOtherApps(true)
in the ViewControllers viewDidAppear()
method.
It fixes everything!
How to open a NSPopover at a distance from the system bar?
First, the reason why button.bounds.offsetBy(dx: 0.0, dy: -20.0)
didn't work is because those coordinate fell outside the "window" of the status bar item which is the status bar itself. So anything outside of it was cropped.
I solved this problem by collecting information here and there:
- Create an invisible window.
- Find the coordinates in the screen of the status bar item and position the invisible window under it.
- Show the
NSPopover
in relation to the invisible window and not the status bar item.
The red thing is the invisible window (for demonstration purposes).
Swift 4 (Xcode 9.2)
// Create a window
let invisibleWindow = NSWindow(contentRect: NSMakeRect(0, 0, 20, 5), styleMask: .borderless, backing: .buffered, defer: false)
invisibleWindow.backgroundColor = .red
invisibleWindow.alphaValue = 0
if let button = statusBarItem.button {
// find the coordinates of the statusBarItem in screen space
let buttonRect:NSRect = button.convert(button.bounds, to: nil)
let screenRect:NSRect = button.window!.convertToScreen(buttonRect)
// calculate the bottom center position (10 is the half of the window width)
let posX = screenRect.origin.x + (screenRect.width / 2) - 10
let posY = screenRect.origin.y
// position and show the window
invisibleWindow.setFrameOrigin(NSPoint(x: posX, y: posY))
invisibleWindow.makeKeyAndOrderFront(self)
// position and show the NSPopover
mainPopover.show(relativeTo: invisibleWindow.contentView!.frame, of: invisibleWindow.contentView!, preferredEdge: NSRectEdge.minY)
NSApp.activate(ignoringOtherApps: true)
}
I was trying to use show(relativeTo: invisibleWindow.frame ...)
and the popup wasn't showing up because NSWindow
is not an NSView
. For the popup to be displayed a view has to be passed.
Change popover size in SwiftUI
I got it to work on iOS with a custom UIViewRepresentable
. Here is what the usage looks like:
struct Content: View {
@State var open = false
@State var popoverSize = CGSize(width: 300, height: 300)
var body: some View {
WithPopover(
showPopover: $open,
popoverSize: popoverSize,
content: {
Button(action: { self.open.toggle() }) {
Text("Tap me")
}
},
popoverContent: {
VStack {
Button(action: { self.popoverSize = CGSize(width: 300, height: 600)}) {
Text("Increase size")
}
Button(action: { self.open = false}) {
Text("Close")
}
}
})
}
}
And here is a gist with the source for WithPopover
Related Topics
Why Is The Swiftui List Not Lazy (Compared to Lazyvstack)
Swift3 Different Font in The All of The UIview with Localization Each
Replicate Opengl Blending in Metal
Updating Existing Constraints Does Not Work, Wrong Use of .Active Property
What's a Placeholder? Why Do We Need to Define Placeholder Data Type for Generics
Skipnext Skipprevious Google Cast Greyed Out
How to Show Cluster for Mkpolyline with Geojson Data View in iOS Swift
Find Index of Annotation Mapkit
Uipageviewcontroller with Previous and Next Buttons
Heightanchor.Constraint Not Change Height of View
How to Center Nspopover When Using Swiftui
Protocol Variable Implementing Another Protocol
How to Draw Something on a PDF in Swift