I'm trying to set a UIView at the bottom of the content of a UIScrollView, do to so I set the view's position to the scrollview's contentsize height. But my scrollview is a subview of a UIWebView so when images are loaded, the contentsize height changes and my view which should be at the bottom of the scrollview ends up in the middle...
So I am looking for a way to be notified when the scrollview's contentsize changes. I've tried to subclass it and change the setter for contentsize to send a NSNotification:
@implementation UIScrollView (Height)
-(void)setContentSize:(CGSize)contentSize
{
_contentSize=contentSize;
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"scrollViewContentSizeChanged" object:nil]];
}
@end
But on compile I get and error saying:
"_OBJC_IVAR_$_UIScrollView._contentSize", referenced from: -[UIScrollView(Heigth) setContentSize:] in MyClass.o ld: symbol(s) not found for architecture armv7
Any idea how the setter should be subclassed ?
Thanks !
Perhaps you can use key-value observing (KVO) to detect changes to the content size. I haven't tried it, but the code should look like this:
If that doesn't work, you may need to swizzle the
setContentSize:
method. Method swizzling lets your replacement method call the original method, which is what you need to do to pass the new content size on to the scroll view.You can read more about method swizzling here: http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html
I think this is the most popular code for swizzling: https://github.com/rentzsch/jrswizzle
Your approach is half correct. You can surely override an existing method through a category, what you cannot do, though is accessing an ivar of the class.
In this case, what you need is method swizzling: you override
setContentSize
while at the same time keeping a reference to the original implementation of the method, so you can call it to set_contentSize
value.Here is the code that you could use, with comments:
Hope it helps.