On iOS,If we want to display a shadow View,So basically do:
1 2 3 4 5 |
self.view.layer?.shadowColor = NSColor.black.cgColor self.view.layer?.shadowOpacity = 0.1 self.view.layer?.shadowOffset = CGSize(width: 0, height: 0) self.view.layer?.shadowRadius = 3 self.view.layer?.masksToBounds = false |
but,To the macOS,This would not work - no effect。
The answer lies in the macOS a View If you want to use layer ,You must manually mark,For example, this:
1 |
self.view.wantsLayer = true |
This time,What if you are 10.13,Then everything will be fine,Then 10.12 The discovery - still no effect。
Why is this? Apple's official guide,We can find the answer,He says you should not be modified directly NSView of layer Attributes,So you should first create a NSShadow() Example,Then modify its properties,Finally, assign it to NSView 。For example, the following code,It is the proper operation:
1 2 3 4 5 6 7 8 9 10 11 |
self.view.wantsLayer = true let shadow = NSShadow() shadow.shadowColor = NSColor.black.cgColor shadow.shadowOpacity = 0.1 shadow.shadowOffset = CGSize(width: 0, height: 0) shadow.shadowRadius = 3 self.view.shadow = shadow self.view.layer?.backgroundColor = NSColor.white.cgColor self.view.layer?.cornerRadius = 8 self.view.layer?.masksToBounds = false |
or,You can also simply point,Give layer One shadow Example,Then go to modify them。
1 2 3 4 5 6 7 8 9 10 |
self.view.wantsLayer = true self.view.shadow = NSShadow() self.view.layer?.backgroundColor = NSColor.white.cgColor self.view.layer?.cornerRadius = 8 self.view.layer?.shadowColor = NSColor.black.cgColor self.view.layer?.shadowOpacity = 0.1 self.view.layer?.shadowOffset = CGSize(width: 0, height: 0) self.view.layer?.shadowRadius = 3 self.view.layer?.masksToBounds = false |
Original article written by LogStudio:R0uter's Blog » In NSView 10.12 Detailed problem does not show shadow
Reproduced Please keep the source and description link:https://www.logcg.com/archives/2946.html