-
Notifications
You must be signed in to change notification settings - Fork 389
Clear / Transparent background for NavigationSplitView #378
-
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
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
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 AMHowever, 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
Beta Was this translation helpful? Give feedback.