在layoutSubviews中调用setNeedsDisplay吗? [英] Calling setNeedsDisplay in layoutSubviews?

查看:94
本文介绍了在layoutSubviews中调用setNeedsDisplay吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在 drawRect:中绘制的具有自定义背景的视图。如果视图改变大小,则需要重新绘制背景。

Consider a view with a custom background that is drawn in drawRect:. If the view changes size, the background need to be redrawn.

这是个坏主意吗?

- (void) layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}

如果是这样,考虑到我无法控制,这将是更好的选择谁更改了视图的大小?

If it is, what would be a better alternative considering I can't control who changes the size of the view?

推荐答案

不要这样做,这是没有必要的。将视图的 contentMode 设置为 UIViewContentModeRedraw

Dont do that, it's not necessary. Set the contentMode of the view to UIViewContentModeRedraw:


UIViewContentModeRedraw

UIViewContentModeRedraw

当边界通过调用setNeedsDisplay方法更改时重新显示视图。
在iOS 2.0和更高版本中可用。
在UIView.h中声明。

Redisplays the view when the bounds change by invoking the setNeedsDisplay method. Available in iOS 2.0 and later. Declared in UIView.h.

这将达到相同的效果。

这篇关于在layoutSubviews中调用setNeedsDisplay吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆