Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Clear / Transparent background for NavigationSplitView #378

Unanswered
mikechambers asked this question in Q&A
Discussion options

I am trying to use the library to set the background of a navigation split view to be clear / transparent on MacOS

Looking at code here, which does it:
https://stackoverflow.com/questions/74838683/is-there-any-way-to-make-navigationsplitviews-side-bar-with-clear-background

 .introspectSplitViewController { controller in
 controller.viewControllers[0].view.backgroundColor = UIColor.clear
 
 for subview in controller.viewControllers[0].view.subviews {
 subview.backgroundColor = UIColor.clear
 for innerSubview in subview.subviews {
 innerSubview.backgroundColor = UIColor.clear
 for innerInnerSubview in innerSubview.subviews {
 innerInnerSubview.backgroundColor = UIColor.clear
 }
 }
 }
 }

But this seems to be out of date now, and im having trouble finding how to set the background color to clear (Im not as familiar with UIKit). Any tips, suggestions or pointers?

I also saw this post:
https://github.com/davdroman/NavigationSplitViewRemoveBackgrounds/blob/main/NavigationSplitViewRemoveBackgrounds.swiftpm/MyApp.swift

but that is iOS and im having trouble getting it working on MacOs

You must be logged in to vote

Replies: 1 comment

Comment options

ok. Here is what I have thus far:

			.introspect(.navigationSplitView, on: .macOS(.v14)) { split in
				
				let c = split.delegate as! NSViewController
				let removeBackgrounds = {
					
					c.children.forEach { controller in
						controller.parent?.view.wantsLayer = true
						controller.parent?.view.layer?.backgroundColor = bgColor
						controller.view.clearBackgrounds()
					}
				}
				removeBackgrounds() // run now...
				DispatchQueue.main.async(execute: removeBackgrounds) // ... and on the next run loop pass
			}

and

let bgColor = NSColor.clear.cgColor
private extension NSView {
	func clearBackgrounds() {
		wantsLayer = true
		layer?.backgroundColor = bgColor
		for subview in subviews {
			subview.clearBackgrounds()
		}
	}
}

I i set the color to something like blue, then everything except title bar is blue:

Screenshot 2023年10月18日 at 11 51 03 AM

However, if I set to clear, it doesnt work for sidebar / titlebar:

Screenshot 2023年10月18日 at 11 52 04 AM

(The image is the background in a zstack)

I feel like im getting close, just missing something obvious.

btw, I am able to set the opacity on the views, but then that also sets the opacity for the content.

Edit I posted a simplified project here with current code:
https://github.com/mikechambers/TransparentNavigationSplitViewExample

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant

AltStyle によって変換されたページ (->オリジナル) /