Calling layer.setNeedsDisplay() shows a black back

2019-07-25 14:05发布

When I call setNeedsDisplay() on my UIView's CALayer (either directly, or via needsDisplayOnBoundsChange), it draws the background as black.

myView.layer.setNeedsDisplay()

When I call 'setNeedsDisplay()' on the UIView directly, it doesn't show up as black:

myView.setNeedsDisplay()

Why does this happen, and how can I fix it?

enter image description here

1条回答
趁早两清
2楼-- · 2019-07-25 14:57

The problem was that my view's background color was somehow set to nil instead of .white or .clear, for example.

It seems odd that this happens only when calling it on the layer and not the view.

To reproduce...

This shows up as white:

myView.backgroundColor = nil
myView.setNeedsDisplay()

This causes the view to display as black:

myView.backgroundColor = nil
myView.layer.setNeedsDisplay()
查看更多
登录 后发表回答