I have a UIRefreshControl subclass that in addition to the title adds a custom control to its subviews. It works great except of one thing, the pull-based-on-distance-opacity is applied only to the text, and not the subview.
class CustomRefreshControl: UIRefreshControl {
private let customView = MyCustomView()
override init() {
super.init()
addSubview(customView)
}
How can I dynamically change the customView opacity based on the UIRefreshControl pull distance? I want it to be the same as the attributedTitle opacity.
I don't think there is a dedicated way to match the opacity of the attributed title. I don't even think that
UIRefreshControlis designed to be modified in this way, adding random subviews to it.I would suggest creating your own view that resembles a
UIRefreshControl, that changes its opacity and frame depending on the scroll view'scontentOffset. See here for a starting point.Assuming a vertical-scroll only scroll view, you could do something like:
I wouldn't recommend this, but a really hacky solution would be to inspect the view hierarchy of
UIRefreshControland find that it has a subview of type_UIRefreshControlModernContentView. The underscore prefix suggests that this is not a stable API, so could break in future iOS versions.As a subview of
_UIRefreshControlModernContentView, there is aUILabeldisplaying theattributedTitle. You can constantly try to set that label'slayer.opacityto your custom view'slayer.opacity.Since the label for
attributedTitleappears to move when you scroll,layoutSubviewswill be called, and you can do this in there: