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?

推荐答案

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

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?

original answer:

You can use key-value observing:

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

and implement:

- (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:
    }
}

Ios相关问答推荐

SwiftUI文本编辑器内部滚动填充,无需文本裁剪

RN 0.63支持iOS ProMotion?

UIImage大小调整工作,但转换为上传数据时恢复到原始大小

iPhone:iOS 17.4中没有气压数据

AVFoundation Camera推出变焦SWIFT

无法添加以供审阅-Xcode 15.0.1

如何在KMM项目中处理PHPickerViewController的回调?

CIKernel的Metal Shader裁剪问题

如何使用Swift以编程方式更改SVG文件中某些元素的 colored颜色

如何在用户点击的位置使用SceneKit渲染球体?

NavigationLink 只能在文本部分点击

集成 IDNow SDK 时未找到 Xcode FLAnimatedImage.h 文件问题

我可以在 Apple-Watch 上使用 iPhone 的摄像头扫描 SwiftUi 中的二维码(例如用于登录)吗

将视图(例如 Text())不透明度设置为 0 或将背景设置为 Color.clear 会导致它不被绘制

iPhone 6 和 6 Plus 媒体查询

何时需要将应用程序源包含在测试目标中?

使用 AVAudioPlayer 播放声音

通过 segue 传递数据

Swift:如何在设备旋转后刷新 UICollectionView 布局

UIGestureRecognizer 阻止子视图处理touch 事件