0

I'm trying to create a custom NSWindow where I want my content to completely fill the available space, I have managed to hide the navigation bar buttons and title and make it transparent, but when I set the content to fill the available space there is some space at the bottom that is not working

window with space at the bottom

The way I create my window:

  private func createWindow() -> MyWindow {
    let window = MyWindow(
      contentRect: NSRect(x: 0, y: 0, width: 10, height: 10), // these values don't matter, the window expands to the swiftUI view
      styleMask: [.titled, .closable, .fullSizeContentView],
      backing: .buffered, defer: false)
    
    window.titlebarAppearsTransparent = true
    window.titleVisibility = .hidden
    window.isMovableByWindowBackground = false
    window.isReleasedWhenClosed = false
    window.collectionBehavior = [.transient, .ignoresCycle]
    window.standardWindowButton(.closeButton)?.isHidden = true
    window.standardWindowButton(.zoomButton)?.isHidden = true
    window.standardWindowButton(.miniaturizeButton)?.isHidden = true
    return window
  }

And on my component, I added the following property:

.ignoresSafeArea()

I already followed all the suggestions in this question but nothing seems to work on my case, any help is appreciatted

Oscar Franco
  • 5,691
  • 5
  • 34
  • 56
  • Works fine here - Xcode 13.1 / macOS 12.0.1. Probably there is some issue with SwiftUI view which you place in this window (or with your `MyWindow`, because I tested with `NSWindow`). – Asperi Dec 08 '21 at 12:43
  • I tried with another SwiftUI which is very simple, just a text and a background color and the behavior is still the same, there is some space at the bottom which is not filled properly :( – Oscar Franco Dec 08 '21 at 12:47
  • Also tried with a pure NSWindow, also not working – Oscar Franco Dec 08 '21 at 12:49
  • @Asperi maybe you can provide a running example? – Oscar Franco Dec 08 '21 at 13:00
  • @OscarFranco: what is MyWindow in your code? – ios coder Dec 08 '21 at 13:48
  • I got rid of it, it was just setting canBecomeKey to true – Oscar Franco Dec 08 '21 at 13:50
  • So you are trying to return what then? It seems you have working code on pic – ios coder Dec 08 '21 at 13:51
  • The code on the pic is the component itself, but it is not so important, the instance of the window just gets returned and then a HostingView is created... You should check out the first answer and the comments below, it looks like SwiftUI is broken when dealing with backgrounds on VStacks – Oscar Franco Dec 08 '21 at 13:54
  • You are not showing your used code, you are trying to return NSWindow, the link in your question has deferent approach. How you are trying use NSWindow in SwiftUI? – ios coder Dec 08 '21 at 14:08

1 Answers1

4

It looks I've reproduced what you might have

window.contentView = 
  NSHostingView(rootView: 
     Rectangle().fill(Color.red)
       .ignoresSafeArea()                 // << works !!
       .frame(width: 640, height: 480)
    // .ignoresSafeArea()             // << issue when here !!
  )

More findings: no issue even with ignoresSafeArea at the end when to use min W/H instead of strict frame for window, ie.

.frame(minWidth: 640, minHeight: 480)
.ignoresSafeArea()
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • weird... not working for me (using a VStack and a text inside)... could it also be because I'm using a real swiftUI struct in its own file? – Oscar Franco Dec 08 '21 at 13:15
  • I have doubts in that. The issue is really with `ignoresSafeArea` modifier. – Asperi Dec 08 '21 at 13:16
  • Well... I just tried with VStack and background and it seems like drawing the background is the thing that fails? VStack { Rectangle().fill(Color.red) .frame(width: 200, height: 200) } .ignoresSafeArea() // << works !! .frame(width: 640, height: 480) .background(Color.blue) – Oscar Franco Dec 08 '21 at 13:22
  • See updated part - as tested it works even with .background at the end. – Asperi Dec 08 '21 at 13:27
  • Still not working for me on a VStack, but it does work if I use a ZStack and draw the rectangle manually as a background – Oscar Franco Dec 08 '21 at 13:30