I am trying some stuffs out with CATiledLayer inside UIScrollView.
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.
Is there a way to detect when the size of UIView(either frame, bounds) or the contentSize of UIScrollView is resized?
I tried
override var frame: CGRect {
didSet {
println("frame changed");
}
}
inside UIView subclass,
but it is only called once when the app starts, although the size of UIView is resized afterwards.
STEP 1:viewWillLayoutSubviews
STEP 2:viewDidLayoutSubviews
Above these methods are called whenever bounds of UIView is changed
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.
viewWillLayoutSubviews()
andviewDidLayoutSubviews()
will be called whenever the bounds change. In the view controller.There's an answer here:
https://stackoverflow.com/a/27590915/5160929
Just paste this outside of a method body:
You can also use KVO:
You can set a KVO like this, where
view
is theview
you want to observe frame changes for:And you can get the changes with this notification:
The
observeValueForKeyPath
will be called whenever the frame of the view you are observing changes.Also remember to remove the observer when your view is about to be deallocated: