是否有UIView调整大小事件? [英] Is there a UIView resize event?

查看:106
本文介绍了是否有UIView调整大小事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图像视图行和列的视图。

I have a view that has rows and columns of imageviews in it.

如果调整此视图的大小,我需要重新排列图像视图位置。

If this view is resized, I need to rearrange the imageviews positions.

此视图是另一个调整大小的视图的子视图。

This view is a subview of another view that gets resized.

有没有办法检测何时调整此视图的大小?

Is there a way to detect when this view is being resized?

推荐答案

正如Uli在下面评论的那样,正确的方法是覆盖 layoutSubviews 并在那里布局imageView。

As Uli commented below, the proper way to do it is override layoutSubviews and layout the imageViews there.

如果由于某种原因,你不能子类化并覆盖 layoutSubviews ,观察界限应该有效,即使是有点脏。更糟糕的是,观察存在风险 - Apple不保证KVO能够在UIKit课程中使用。请阅读Apple工程师的讨论:关联对象什么时候发布?

If, for some reason, you can't subclass and override layoutSubviews, observing bounds should work, even when being kind of dirty. Even worse, there is a risk with observing - Apple does not guarantee KVO works on UIKit classes. Read the discussion with Apple engineer here: When does an associated object get released?

原始答案:

您可以使用键值观察:

You can use key-value observing:

[yourView addObserver:self forKeyPath:@"bounds" options:0 context:nil];

并实施:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (object == yourView && [keyPath isEqualToString:@"bounds"]) {
        // do your stuff, or better schedule to run later using performSelector:withObject:afterDuration:
    }
}

这篇关于是否有UIView调整大小事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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