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

查看:22
本文介绍了是否有 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 并在那里布局 imageViews.

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

如果由于某种原因,你不能子类化和覆盖 layoutSubviews,观察 bounds 应该可以工作,即使有点脏.更糟糕的是,观察存在风险——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?

原答案:

您可以使用键值观察:

[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天全站免登陆