How can I create a window with transparent background with swift on osx?
NSWindow has a property 'opaque' which it is true by default.
The value of this property is true when the window is opaque;
otherwise, false.
Just change it to false:
override func viewWillAppear() {
super.viewWillAppear()
view.window?.opaque = false
view.window?.backgroundColor = NSColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.5)
}
Swift 4 update: opaque
has been renamed isOpaque
override func viewWillAppear() {
super.viewWillAppear()
view.window?.isOpaque = false
view.window?.backgroundColor = NSColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.5)
}
How to make window transparent in osx swift?
You can get an instance of your application window as follow:
guard let window = NSApplication.shared().windows.first else { return }
window.isOpaque = false
window.backgroundColor = .clear
Get your window from NSApp (global constant for the shared app instance):
guard let window = NSApp.windows.first else { return }
Or override viewWillAppear
or viewDidAppear
and access your view's window property.
override func viewWillAppear() {
super.viewWillAppear()
view.window?.isOpaque = false
view.window?.backgroundColor = .clear
}
Swift ui macos background transparent TextField
You need visual effect view in background (it is used by default for sidebar styled lists)
Demo prepared & tested with Xcode 11.4 / macOS 10.15.6
struct VisualEffectView: NSViewRepresentable {
func makeNSView(context: Context) -> NSVisualEffectView {
let view = NSVisualEffectView()
view.blendingMode = .behindWindow // << important !!
view.isEmphasized = true
view.material = .appearanceBased
return view
}
func updateNSView(_ nsView: NSVisualEffectView, context: Context) {
}
}
and put it to needed area, in this case below TextField
TextField("Username", text: $username)
.padding(.leading, 20)
.padding(.trailing, 20)
.background(
RoundedRectangle(cornerRadius: 5)
.fill(Color.white.opacity(0.3)
)
.padding(.leading, 20)
.padding(.trailing, 20)
)
.padding(.top)
.padding(.bottom)
.background(VisualEffectView())
Making the title bar of a window completely transparent
The problem is that inside viewDidLoad method the window property is always nil. All the optional chaining in your code just fails silently. You just need to move your code to viewWillAppear method.
override func viewWillAppear() {
super.viewWillAppear()
// configure your window properties here
}
Transparent background WKWebView (NSView)
It was not supported, then they fixed it:
https://bugs.webkit.org/show_bug.cgi?id=134779
The way to make it transparent is to:
myWebView.opaque = false
Swift - Cocoa - Change window color?
Ok, so I am assuming you are using Storyboards here (because otherwise the template Xcode creates would have given you a window
property in the AppDelegate
and you'd probably would have successfully tried setting the background color on that).
There are many options how to do this but basically you would use an NSWindow
subclass or an NSWindowController
subclass to be responsible for setting the windows appearance (for those properties that can not be set in Interface Builder at least)
NSWindow subclass:
class BlackWindow: NSWindow {
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
backgroundColor = .black
}
}
and set this subclass as the class in the storyboard.
NSWindowController subclass:
class BlackWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
window?.backgroundColor = .black
window?.titlebarAppearsTransparent = true
}
}
and again set the class in the storyboard. There are many variants of where you can modify your window in the window controller, windowDidLoad()
is just one suggestion.
Hope this helps.
Transparent background NSWindow was not recovered
For a window with transparent parts, you need to invalidate the shadow after you change what has been drawn. The shadow is computed from the opaque (or mostly-opaque) parts and therefore depends on exactly what gets drawn.
So, after you change the way it draws, whether that has to do with hiding views or redrawing them, you need to call invalidateShadow()
on the window.
How to make NSTableView transparent?
Got Answer..!!! I just tried this
[tableview setBackgroundColor:[NSColor clearColor]];
[tableview setHeaderView:nil];
and its working fine.. – – Snehal
Copied from comment in question, as its a bit buried...
Make sidebar widget transparent on macOS Mojave
I just found a solution in the Apple Developer forums:
Apple Developer Forum
In the info.plist file of the extension, there is a section called NSExtension. In there, the NSExtensionAttributes/NSExtensionPointVersion had to be incremented (was 2.0, now is 3.0).
Now the app extension looks nice:
Related Topics
How to Bind Different Associated Values in a Swift Enum to the Same Var
"Generic Parameter Could Not Be Inferred" in Swiftui Uiviewrepresentable
I Won't Be Able to Return a Value with Alamofire in Swift
Extending Collection with a Recursive Property/Method That Depends on the Element Type
Swiftui - Wait Until Firestore Getdocuments() Is Finished Before Moving On
Self' Captured by a Closure Before All Members Were Initialized
Can an Enum Contain Another Enum Values in Swift
Get an Array of Dates of the Current Week Starting on Monday
How to Handle Closure Recursivity
Count Elements of Array Matching Condition in Swift
How to Test If Objects Conforming to the Same Protocol Are Identical in Swift Without Casting
Parse.Com Querying User Class (Swift)
Firestore Order by Time But Sort by Id
Initialize Lazy Instance Variable with Value That Depends on Other Instance Variables
Convert Generic Free Function into Array Extension
What Determines Whether a Swift 5.5 Task Initializer Runs on the Main Thread