I want to watch for changes in a UIView
's frame
, bounds
or center
property. How can I use Key-Value Observing to achieve this?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
Currently it's not possible to use KVO to observe a view's frame. Properties have to be KVO compliant to be observable. Sadly, properties of the UIKit framework are generally not observable, as with any other system framework.
From the documentation:
There are a few exceptions to this rule, like NSOperationQueue's
operations
property but they have to be explicitly documented.Even if using KVO on a view's properties might currently work I would not recommend to use it in shipping code. It's a fragile approach and relies on undocumented behavior.
To not rely on KVO observing you could perform method swizzling as follows:
Now to observe frame change for a UIView in your application code:
As mentioned, if KVO doesn't work and you just want to observe your own views which you have control over, you can create a custom view that overrides either setFrame or setBounds. A caveat is that the final, desired frame value may not be available at the point of invocation. Thus I added a GCD call to the next main thread loop to check the value again.
There are usually notifications or other observable events where KVO isn't supported. Even though the docs says 'no', it is ostensibly safe to observe the CALayer backing the UIView. Observing the CALayer works in practice because of its extensive use of KVO and proper accessors (instead of ivar manipulation). It's not guaranteed to work going forward.
Anyway, the view's frame is just the product of other properties. Therefore we need to observe those:
See full example here https://gist.github.com/hfossli/7234623
NOTE: This is not said to be supported in the docs, but it works as of today with all iOS versions this far (currently iOS 2 -> iOS 11)
NOTE: Be aware that you will receive multiple callbacks before it settles at its final value. For example changing the frame of a view or layer will cause the layer to change
position
andbounds
(in that order).With ReactiveCocoa you can do
And if you only want to know when
bounds
changes you can doAnd if you only want to know when
frame
changes you can doIf I might contribute to the conversation: as others have pointed out,
frame
is not guaranteed to be key-value observable itself and neither are theCALayer
properties even though they appear to be.What you can do instead is create a custom
UIView
subclass that overridessetFrame:
and announces that receipt to a delegate. Set theautoresizingMask
so that the view has flexible everything. Configure it to be entirely transparent and small (to save costs on theCALayer
backing, not that it matters a lot) and add it as a subview of the view you want to watch size changes on.This worked successfully for me way back under iOS 4 when we were first specifying iOS 5 as the API to code to and, as a result, needed a temporary emulation of
viewDidLayoutSubviews
(albeit that overridinglayoutSubviews
was more appropriate, but you get the point).There is a way to achieve this without using KVO at all, and for the sake of others finding this post, I'll add it here.
http://www.objc.io/issue-12/animating-custom-layer-properties.html
This excellent tutorial by Nick Lockwood describes how to use core animations timing functions to drive anything. It's far superior to using a timer or CADisplay layer, because you can use the built in timing functions, or fairly easily create your own cubic bezier function (see the accompanying article (http://www.objc.io/issue-12/animations-explained.html) .