问题
I'm failing in creating a simple macSO app with Xcode 9 because of a very ... and I mean very strange behaviour. I have two views with simple layers to draw different background colours. Like this: When I run the app, without changing anything, after (usually) the third run in a row, the views disappear ... literally. And the window looks like this: Here is a test project:
https://github.com/ivailon/test13
Furthermore - if I start executing the already build app, again the views sometimes disappear. Can anyone give even a clue of a reasonable explanation why this is happening ...
回答1:
The problem is that not every time the view's layer is initialised before the call of:
func viewDidLoad()
Description
Called after the view controller’s view has been loaded into memory.
To fix this one should initialise the layer later or create its own CALayer object and setup the view with it, like this:
let layer = CALayer()
layer.backgroundColor = NSColor.orange.cgColor
view1.layer = layer
view1.wantsLayer = true
The project has been updated at GitHub to show the difference.
来源:https://stackoverflow.com/questions/46394748/xcode-9-compiled-app-with-unusual-behaviour