Xcode 9 compiled app with unusual behaviour

隐身守侯 提交于 2019-12-11 17:08:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!