While I am exploring the option to observe a UIView
's bounds
or frame
change (mentioned here and here), I have encountered a very strange discrepancy: didSet
and willSet
will be triggered differently based on where you put your UIView
in the view hierarchy:
- If I use property observer for
UIView
at the root of a view controller, I will only getdidSet
andwillSet
events fromframe
changes. - If I use property observer for
UIView
that is a subview inside a view controller, I will only getdidSet
andwillSet
events frombounds
changes.
I’d like to note first that I’m explicitly avoiding KVO approach mentioned here since it’s not officially supported. I’m also not looking to use viewDidLayoutSubviews()
mentioned here since that won’t work for observing changes of subviews (see the doc). This question assumes my preference to use didSet
and willSet
to observe a UIView
’s bounds
/ frame
changes.
The closest question I have come across is this question but it covers only the initialization phrase, and also doesn’t mention the case of observing a subview.
Details
To see this in action, check out
From my repost in Apple Developer Forum,
QuinceyMorris
helped me clarifying issues with this approach as well as an approach that would work no matter where I put the view in the view hierarchy.Based on his recommendation that I override
layoutSubviews
, here's my updated subclass (just like this answer):