How to Hide The Top Bar (With Buttons) Usin Swift and Macos

How to hide the top bar (with buttons) usin Swift and MacOS?

What you want is a window with a style mask of type NSWindowStyleMaskBorderless. The problem is that this has to be set when initializing the window. That's why your line self.window?.styleMask = .borderless has no effect.

Because you are creating your window using Interface Builder, you have to open the corresponding xib file (I guess MainMenu.xib), select the window and deselect Title Bar in the Attributes inspector:

Title Bar

I know. This is confusing. But at least Apple mentions it in the documentation:

Note that you can set a window’s or panel’s style mask to NSWindowStyleMaskBorderless in Interface Builder by deselecting Title Bar in the Appearance section of the Attributes inspector.

If you want to learn more about the different styles of a NSWindow checkout NSWindowStyles by Luka Kerr. That's a great reference.

Update: In said reference i found another way to remove the title bar that might work for you:

window?.styleMask.remove(.titled)

Xcode & Swift - Window without title bar but with close, minimize and resize buttons

If you are using storyboard, it's just a simple check box in the Inspector bar.

  1. Select the window from Story Board
    Sample Image

  2. Check the Transparent Title Bar checkbox in the inspector window.

Sample Image

Here's how it looks like in the Story board. It looks the same when you build and run the application.

Sample Image

Swift/OS X - Remove Application Title Bar and Add Custom Close Button

To show/hide window buttons would want to set the visibility of NSWindowButton:

These constants provide a way to access standard title bar buttons:

enum NSWindowButton : UInt {
case CloseButton
case MiniaturizeButton
case ZoomButton
case ToolbarButton
case DocumentIconButton
case DocumentVersionsButton
case FullScreenButton
}

So you would likely use something such as this to set this visibility:

self.window!.standardWindowButton(NSWindowButton.CloseButton)?.hidden = true

Any other constants you want to prevail would likely work the same way. You would then set your new custom close button for example to the applications first responder terminate function.

How do I hide the status bar in a Swift iOS app?

You really should implement prefersStatusBarHidden on your view controller(s):

Swift 3 and later

override var prefersStatusBarHidden: Bool {
return true
}

iOS Swift - How to remove top bar back button's Back text?

This will work for you

self.navigationController?.navigationBar.topItem?.title = " "

How to close/dismiss/hide a menu by clicking on a button in an embedded view from within it, in Swift?

Inspired by El Tomato's comment, I found the solution.

Given the fact that the plusButtonClicked is limited to its own context, which is the controller within which it resides and all the public variables, I could not call a method on CustomMenu from it. Because CustomMenu in itself is not public. But its containing variable statusBarItem.menu, is public and accessible from all the other views. So I added statusBarItem.menu?.cancelTracking() to plusButtonClicked action and it works.

NSWindow - hiding titlebar while keeping control buttons with Cocoa/AppKit (NOT SwiftUI)

You can accomplish what you are looking for by setting the window's title bar to transparent, hide its title text and set its appearance to Full Size Content View.

Sample Image

Sample Project

If you would like to have your table view ignore the top inset you need to set its enclosing scroll view automaticallyAdjustsContentInsets property to false

Sample Image

scrollView.automaticallyAdjustsContentInsets = false

How to hide a navigation bar from first ViewController in Swift?

If you know that all other views should have the bar visible, you could use viewWillDisappear to set it to visible again.

In Swift:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.setNavigationBarHidden(false, animated: animated)
}

How do I show/hide a UIBarButtonItem?

Save your button in a strong outlet (let's call it myButton) and do this to add/remove it:

// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy];

// This is how you remove the button from the toolbar and animate it
[toolbarButtons removeObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];

// This is how you add the button to the toolbar and animate it
if (![toolbarButtons containsObject:self.myButton]) {
// The following line adds the object to the end of the array.
// If you want to add the button somewhere else, use the `insertObject:atIndex:`
// method instead of the `addObject` method.
[toolbarButtons addObject:self.myButton];
[self setToolbarItems:toolbarButtons animated:YES];
}

Because it is stored in the outlet, you will keep a reference to it even when it isn't on the toolbar.



Related Topics



Leave a reply



Submit