I'm creating an application for OSX and the only thing I need is to delete the background color of my application's window.
If I run the app, it will appear a window that could be white or black (Aqua, Dark Aqua).
So, i tried to move into the 'ViewController.swift' file and I wrote this code
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// tell the controller's view to use a CALayer as its backing store
view.wantsLayer = true
// change the background color of the layer
view.layer?.backgroundColor = NSColor.red.cgColor
}
}
For example, in this way I turn the background colour to red. BUT, this is not what I want. So I tried another way (using RGBA colours)
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// tell the controller's view to use a CALayer as its backing store
view.wantsLayer = true
// change the background color of the layer
view.layer?.backgroundColor = CGColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
}
}
And I saw that decreasing the 'alpha' value, the opacity decreases too. So, I tried with
CGColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 0)
The red colour disappears, but now you can see the white (or black) colour of the ViewController.
I understood that I just create another layer (like an ImageView) with a colour, but I haven't modified the ViewController background. Is there a way I can do this? Or it is just impossible?
I've found out the solution. Just go to the "ViewController.swift" file and than write this
class ViewController: NSViewController {
override func viewWillAppear() {
super.viewWillAppear()
view.window?.isOpaque = false
view.window?.backgroundColor = NSColor(red: 1, green: 0.5, blue: 0.5, alpha: 0.0)
}
}
If you wanna change the colour, just fix the red, green, blue values. The alpha value determinate the transparency.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.