Search code examples
swiftcocoawindowtitlebar

Change position of traffic lights with swift


I am new to swift and am trying to make a MacOS app. I have a transparent title bar and am wondering how to offset the traffic lights. I want to achieve a similar effect to the one which electron uses with the hidden-inset option. Basically, I want the title bar effect which can be seen here. Notice how the traffic lights are a bit more offset from the top and left corners of the windows than normal.

Just in case you guys are wondering, here is some code I have which changes some of the title bar settings:

override func viewWillAppear() {
    super.viewWillAppear();

    self.view.window?.titlebarAppearsTransparent = true;
    self.view.window?.isMovableByWindowBackground = true;
    self.view.window?.setIsZoomed(true);
}

This is pretty much the only code I have added to my project so far. I want to get the window changes down before moving on.

Any help would go a long way! Thanks


Solution

  • Generally speaking, it's not a good idea to move the traffic lights physically.

    However, if you just want to make your title bar like the screenshot you presented, you can do it by setting .titleVisibility hidden:

    window.titleVisibility = .hidden
    

    enter image description here

    Then the upper part of the title bar will be hidden and the traffic lights are placed at vertically center of the title bar, in the same level of toolbar icons, and you need to put the window title somewhere by youself (if you want).