1

As a SwiftUI beginner, I have just recently started creating my first MacOS app. However, when I was trying to implement the NSVisualEffectView to blur the background, the contentView that I was using went off of the screen, which wasn't visible at all. It looked something like this:

enter image description here

So I tried fixing the size of the text I had in the contentView by making the text I had into Text("Hello world!").frame(width: 700, height:500), and the screen became like this:

enter image description here

By doing this, in the bottom left corner, the tiny shapes of Hello world! can be seen. However, unless I position the text on the top right of the contentView, I can't seem to move it. Does anyone know how to fix this?

*For reference, here is the AppDelegate.swift contents:

import SwiftUI

@main
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: NSWindow!


    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()
        let visualEffect = NSVisualEffectView()
        visualEffect.blendingMode = .behindWindow
        visualEffect.state = .active
        visualEffect.material = .fullScreenUI
        visualEffect.addSubview(NSHostingView(rootView: contentView))

        // Create the window and set the content view.
        window = NSWindow(
            contentRect: .zero,
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.isReleasedWhenClosed = false
        window.center()
        window.setFrameAutosaveName("Main Window")
        window.contentView = visualEffect
        window.makeKeyAndOrderFront(nil)
        window.titlebarAppearsTransparent = true
        window.titleVisibility = .hidden
        
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }
    
} 
koen
  • 5,383
  • 7
  • 50
  • 89
i3ta
  • 11
  • 2
  • 1
    Doest this answer your question https://stackoverflow.com/a/63669868/12299030? – Asperi Nov 22 '20 at 15:15
  • In my case, my biggest problem is that the view that I have (the default "hello world") is going off of the screen. That question doesn't exactly solve my problem because I want the entire window to be blurred, including the title bar... – i3ta Nov 22 '20 at 15:47
  • Update: @Asperi the solution you posted works for the background, but I was only able to implement it on `contentView()`, which meant that the title bar appears again. Is there any way to change that? – i3ta Nov 23 '20 at 12:37
  • You just need to hide it, like in https://stackoverflow.com/a/60252103/12299030. – Asperi Nov 23 '20 at 12:50
  • @Asperi That works, except now the bottom shows the background? – i3ta Nov 23 '20 at 13:11
  • Update: I found a fix where I set `maxWidth` and `maxHeight` to `.infinity`, which fixed it. Not sure why though. – i3ta Nov 23 '20 at 13:30

0 Answers0