ios - 如何在swift ios xcode中检测UIView大小何时更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30616418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:11:46  来源:igfitidea点击:

How to detect when UIView size is changed in swift ios xcode?

iosswiftcocoa-touchuiviewuiscrollview

提问by Joon. P

I am trying some stuffs out with CATiledLayer inside UIScrollView.

我正在 UIScrollView 中使用 CATiledLayer 尝试一些东西。

Somehow, the size of UIView inside the UIScrollView gets changed to a large number. I need to find out exactly what is causing this resize.

不知何故, UIScrollView 内的 UIView 的大小被更改为一个很大的数字。我需要确切地找出导致这种调整大小的原因。

Is there a way to detect when the size of UIView(either frame, bounds) or the contentSize of UIScrollView is resized?

有没有办法检测 UIView(框架、边界)的大小或 UIScrollView 的 contentSize 何时调整大小?

I tried

我试过

override var frame: CGRect {
    didSet {
        println("frame changed");
    }
}

inside UIView subclass,

在 UIView 子类中,

but it is only called once when the app starts, although the size of UIView is resized afterwards.

但它只在应用程序启动时调用一次,尽管 UIView 的大小随后会调整大小。

回答by bearacuda13

There's an answer here:

这里有一个答案:

https://stackoverflow.com/a/27590915/5160929

https://stackoverflow.com/a/27590915/5160929

Just paste this outside of a method body:

只需将其粘贴到方法主体之外:

override var bounds: CGRect {
    didSet {
        // Do stuff here
    }
}

回答by simons

viewWillLayoutSubviews()and viewDidLayoutSubviews()will be called whenever the bounds change. In the view controller.

viewWillLayoutSubviews()并且viewDidLayoutSubviews()会在边界改变时被调用。在视图控制器中。

回答by Lefteris

You can also use KVO:

您还可以使用 KVO:

You can set a KVOlike this, where viewis the viewyou want to observe frame changes for:

您可以设置一个志愿这个样子,哪里view就是view你要观察帧之间的变化为:

self.addObserver(view, forKeyPath: "center", options: NSKeyValueObservingOptions.New, context: nil)

And you can get the changes with this notification:

您可以通过此通知获得更改:

override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: NSDictionary!, context: CMutableVoidPointer) {
    }

The observeValueForKeyPathwill be called whenever the frame of the view you are observing changes.

observeValueForKeyPath会叫你正在观察变化的视图一旦帧。

Also remember to remove the observer when your view is about to be deallocated:

还要记住在您的视图即将被释放时移除观察者:

view.removeObserver(self, forKeyPath:"center")

回答by Joon. P

The answers are correct, although for my case the constraints I setup in storyboard caused the UIView size to change without calling back any detecting functions.

答案是正确的,尽管在我的情况下,我在情节提要中设置的约束导致 UIView 大小发生变化而没有回调任何检测函数。

回答by user3182143

STEP 1:viewWillLayoutSubviews

第 1 步:viewWillLayoutSubviews

Called to notify the view controller that its view is about to layout its subviews

When a view's bounds change, the view adjusts the position of its subviews. Your view controller can override this method to make changes before the view lays out its subviews. The default implementation of this method does nothing.

调用以通知视图控制器其视图即将布局其子视图

当视图的边界发生变化时,视图会调整其子视图的位置。您的视图控制器可以覆盖此方法以在视图布局其子视图之前进行更改。此方法的默认实现不执行任何操作。

STEP 2:viewDidLayoutSubviews

第 2 步:viewDidLayoutSubviews

Called to notify the view controller that its view has just laid out its subviews.

When the bounds change for a view controller's view, the view adjusts the positions of its subviews and then the system calls this method. However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted. Each subview is responsible for adjusting its own layout.

Your view controller can override this method to make changes after the view lays out its subviews. The default implementation of this method does nothing.

调用以通知视图控制器它的视图刚刚布局了它的子视图。

当视图控制器视图的边界发生变化时,视图会调整其子视图的位置,然后系统调用此方法。但是,调用此方法并不表示已调整视图子视图的各个布局。每个子视图负责调整自己的布局。

您的视图控制器可以覆盖此方法以在视图布局其子视图后进行更改。此方法的默认实现不执行任何操作。

Above these methods are called whenever bounds of UIViewis changed

每当UIView 的边界发生变化时,就会调用以上这些方法