Reputation: 79
I am studying MAC OS development and I used Swift and Cocoa to create a window that I want to display outside the screen. My code looks like this:
@IBAction func tapShowSplitWindow(_ sender: Any) {
let initRect = NSRect(x: -100, y: -100, width: 300.0, height: 300.0)
var window = NSWindow(contentRect: initRect,styleMask: .titled, backing: .buffered, defer: false)
window.contentView = NSView()
window.contentView?.wantsLayer = true
window.contentView?.layer?.backgroundColor = NSColor.red.cgColor
window.makeKeyAndOrderFront(self)
}
The window will appear in the bottom left corner of the screen, and I expect it to be outside the screen.
so why? and how to show it outside
Upvotes: 1
Views: 556
Reputation: 242
// Reference: https://stackoverflow.com/questions/35808924/placing-nswindow-at-top-of-screen-without-1-pixel-margin
class FullscreenWindow: NSWindow {
override func constrainFrameRect(_ frameRect: NSRect, to screen: NSScreen?) -> NSRect {
print("Rect: \(frameRect)")
return NSRect(origin: .zero, size: frameRect.size)
}
}
You can override the constrainFrameRect
to set the rect of the window.
Upvotes: 0
Reputation: 15633
window.makeKeyAndOrderFront(self)
moves the window on screen. If you really want the window to be half visible then you can move it back with setFrameOrigin
window.makeKeyAndOrderFront(self)
window.setFrameOrigin(window.frameRect(forContentRect:initRect).origin)
Upvotes: 1