14 Replies
      Latest reply on Nov 12, 2019 7:44 AM by Claude31
      narcisfromgirona Level 1 Level 1 (0 points)

        (macOS and Swift)

         

        I try to make a window with rounded corners but every solution I could find does not work when I remove the title bar (Attributes >  Title Bar)

         

        Also, the window is semi-transparent:

         

                view.window?.isOpaque = false
                view.window?.backgroundColor = NSColor.clear 
                view.window?.backgroundColor = NSColor(white: 1, alpha: 0.8)
        

        (It would be ok if it has the regular corners of any window. I do not need a lot of control over the corner radius if that makes things more complicated)

        • Re: Window with rounded corners an no title bar
          janabanana Level 1 Level 1 (0 points)

          Since your app most likely won't be approved in the app store if it doesn't have a title bar, you might want to go all out custom on your window creation.  Check out this example:

           

          www [dot] cocoawithlove [dot] com/2008/12/drawing-custom-window-on-mac-os-x.html

           

          It is in Obj C but you should be able to translate it to Swift.

          • Re: Window with rounded corners an no title bar
            Claude31 Level 8 Level 8 (6,865 points)

            Thanks to https://stackoverflow.com/questions/42762856/nswindow-with-round-corners-in-swift/42763305

             

            I have come to something pretty close to what you want.

             

            I created a project withn Storyboard.

            The window I want rounded is the initial controller.

             

            There are a few points to take care:

            - even though it seems possible in IB to declare Appearance without titleBar, that creates side effect (like windowDidBecomeMain no more called). That may be the problem mentionned by janabana

            - with the settings I propose, there is still a small line at the top, quasi invidible, but I could not get rid of it.

             

            So, the solution:

            In IB, set the windows as:

            Title : whatever or empty

            Hide title unchecked (will be done in code)

            Appearance: TitleBar ON (see warning above)

            Transparent Title Bar : OFF (will be done in code)

            Full Size Content View: ON

            Shadow: ON

            Visible at Launch: ON

            All other OFF except Restorable

             

            Marke the WindowController as a subclass (see other thread https://forums.developer.apple.com/thread/125315)

             

            class FirstWindowController: NSWindowController, NSWindowDelegate {
            
                var firstAppearance = true
               
                override func windowDidLoad() {
                    super.windowDidLoad()
               
                    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
                }
               
                func windowDidBecomeMain(_ notification: Notification) {
                    if firstAppearance {
                        let newFrame = NSRect(x: 120, y: 100, width: 300, height: 200)
                        self.window?.setFrame(newFrame, display: true)
                       
                        let effect = NSVisualEffectView(frame: newFrame) // NSRect(x: 0, y: 0, width: 0, height: 0))
                        effect.blendingMode = .behindWindow
                        effect.state = .active
                        if #available(OSX 10.14, *) {
                            effect.material = .contentBackground
                        } else {
                            // Fallback on earlier versions
                            effect.material = .dark
                        } 
                        effect.wantsLayer = true
                        effect.layer?.cornerRadius = 30.0
                        effect.layer?.borderColor = .clear
                        self.window?.contentView = effect
                        self.window?.titlebarAppearsTransparent = true
                        self.window?.titleVisibility = .hidden
                        self.window?.isOpaque = false
                        self.window?.backgroundColor = .clear
                    }
                    firstAppearance = false
                }
               
            }
              • Re: Window with rounded corners an no title bar
                janabanana Level 1 Level 1 (0 points)

                I solved the window becoming main by subclassing the window and putting in:

                 

                -(BOOL)canBecomeKeyWindow {
                     return YES;
                }

                 

                Make sure to call makeKeyAndOrderFront on the window.

                 

                The app I mentioned didn't have a title bar, I set the window background color to clear and then loaded an image for the window background.  The image had a ragged edge that look like a piece of paper that had gone through a fire with its borders burned.  As a result, the app window appeared to have a ragged, burned edge.  In reality, the window still had the straight border with square corners but since the window itself was clear, you couldn't see the border.  The app looked really neat. 

                • Re: Window with rounded corners an no title bar
                  Claude31 Level 8 Level 8 (6,865 points)

                  @narcisfromgirona

                   

                  Did you succeed in creating the rounded corner window ?

                    • Re: Window with rounded corners an no title bar
                      narcisfromgirona Level 1 Level 1 (0 points)

                      Hi Calude31. Thank you very much for your help. I tried many things that are close. But all seem to have some drawback. I thought about a different approach. I created a completely transparent window and a transparent bar and buttons. Inside an NSView. This way I can control the aspect and conditions of the NSView much easier than the window. It is not the perfect solution but, by now, it is my favorite option.

                       

                      By the way, I could get rid of the pesky line of the transparent window if I put it in the viewVillAppear. (I still not sure why it works well there and not in ViewDidLoad nor if I call the window with a button.)

                        • Re: Window with rounded corners an no title bar
                          Claude31 Level 8 Level 8 (6,865 points)

                          But all seem to have some drawback

                          Which main drawback ?

                           

                          if I put it in the viewVillAppear.

                           

                          In which controller ? Not the NSWindowController ?

                          If in NSViewController, do you set the view frame and its layer (that would make sense as window is now fully transparent).

                          Can you still seize and move the window around ?

                           

                          Anyway, good you found a way. Don't forget to close the thread.