Calling layer.setNeedsDisplay() shows a black back

2019-07-25 14:25发布

问题:

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?

回答1:

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()