检测对UIWebView的滚动视图的contentSize的更改

我试图在UIScrollView的内容的底部设置一个UIView,所以我把视图的位置设置为scrollview的contensize heigth。 但我的scrollview是一个UIWebView的子视图,所以当图像加载时,contentig heigth改变,我的视图应该是在滚动视图的底部结束在中间…

所以我正在寻找一种方式来通知当滚动视图的contentize变化。 我试图将其子类化,并更改setter为contensize发送一个NSNotification:

@implementation UIScrollView (Height) -(void)setContentSize:(CGSize)contentSize { _contentSize=contentSize; [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"scrollViewContentSizeChanged" object:nil]]; } @end 

但在编译我得到和错误说:

“_OBJC_IVAR _ $ _ UIScrollView._contentSize”,引用自: – MyClass.o中的[UIScrollView(Heigth)setContentSize:] ld:symbol(s)not found for architecture armv7

任何想法如何二传手应分类?

谢谢 !

也许你可以使用键值观察(KVO)来检测内容大小的变化。 我没有尝试过,但代码应该是这样的:

 static int kObservingContentSizeChangesContext; - (void)startObservingContentSizeChangesInWebView:(UIWebView *)webView { [webView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:&kObservingContentSizeChangesContext]; } - (void)stopObservingContentSizeChangesInWebView:(UIWebView *)webView { [webView.scrollView removeObserver:self forKeyPath:@"contentSize" context:&kObservingContentSizeChangesContext]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (context == &kObservingContentSizeChangesContext) { UIScrollView *scrollView = object; NSLog(@"%@ contentSize changed to %@", scrollView, NSStringFromCGSize(scrollView.contentSize)); } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } 

如果这不起作用,您可能需要调整setContentSize:方法。 方法swizzling让你的replace方法调用原来的方法,这是你需要做的将新的内容大小传递到滚动视图。

你可以阅读更多关于在这里调整方法: http : //www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html

我认为这是最stream行的swizzling代码: https : //github.com/rentzsch/jrswizzle

你的方法是正确的。 你可以肯定地通过一个类别覆盖一个现有的方法,但是你不能做的,虽然是访问一个类的伊娃。

在这种情况下,你需要的是方法swizzling:你重写setContentSize同时保持方法的原始实现的引用,所以你可以调用它来设置_contentSize值。

以下是您可以使用的代码,并带有注释:

 @implementation UIScrollView (Height) // -- this method is a generic swizzling workhorse // -- it will swap one method impl with another and keep the old one // under your own impl name + (void)swizzleMethod:(SEL)originalSel andMethod:(SEL)swizzledSel { Method original = class_getInstanceMethod(self, originalSel); Method swizzled = class_getInstanceMethod(self, swizzledSel); if (original && swizzled) method_exchangeImplementations(original, swizzled); else NSLog(@"Swizzling Fault: methods not found."); } //-- this is called on the very moment when the categoty is loaded //-- and it will ensure the swizzling is done; as you see, I am swapping //-- setContentSize and setContentSizeSwizzled; + (void)load { [self swizzleMethod:@selector(setContentSize:) andMethod:@selector(setContentSizeSwizzled:)]; } //-- this is my setContentSizeSwizzled implementation; //-- I can still call the original implementation of the method //-- which will be avaiable (*after swizzling*) as setContentSizeSwizzled //-- this is a bit counterintuitive, but correct! - (void)setContentSizeSwizzled:(CGSize)contentSize { [self setContentSizeSwizzled:contentSize]; [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"scrollViewContentSizeChanged" object:nil]]; } @end 

希望能帮助到你。