简体   繁体   English

UIView setNeedsDisplay是否没有效果?

[英]UIView setNeedsDisplay Has No Effect?

I have a UIView-derived view that is drawn only once and never updated . 我有一个UIView派生的视图,该视图仅绘制一次,并且从未更新

Calling [self setNeedsDisplay] has no effect. 调用[self setNeedsDisplay]无效。 What could cause this? 是什么原因造成的?

It doesn't matter whether the drawing code is in 绘图代码是否在其中都没关系

  • drawRect: , or drawRect:
  • drawLayer:inContext: (with an empty drawRect() defined) drawLayer:inContext:定义了一个空的drawRect())

If drawLayer:inContext is not defined, then drawRect is called. 如果未定义drawLayer:inContext,则调用drawRect。 But just once (during layout). 但是只有一次(在布局期间)。

- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
  // drawing occurs, but just once (during view layout)
}

`- (void)drawRect:(CGRect) rect {
  // empty -- never called
}

A small list of what I've tried: 我尝试过的一小部分:

  • [self setNeedsDisplay] [self setNeedsDisplay]
  • [self.layer setNeedsDisplay] [self.layer setNeedsDisplay]
  • [self.mySubLayer setNeedsDisplay] [self.mySubLayer setNeedsDisplay]
  • self.contentMode = UIViewContentModeRedraw; self.contentMode = UIViewContentModeRedraw;
  • creating the view manually, instead of loading it from a .nib (no effect) 手动创建视图,而不是从.nib加载视图(无效)
  • Removing the use of layers (and sublayers) and using just drawRect (no effect) 删除图层(和子图层)的使用,仅使用drawRect(无效果)

因此,可以将其标记为已回答:确保在主线程上调用UIKit方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM