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

Abe*_*bel 15 user-interface uiscrollview ios contentsize

我正在尝试在UIScrollView的内容底部设置一个UIView,这样做我将视图的位置设置为scrollview的contentsize高度.但是我的scrollview是UIWebView的子视图,因此当加载图像时,内容大小会发生变化,我应该位于scrollview底部的视图最终会在中间...

所以我正在寻找一种方法来在scrollview的contentsize更改时得到通知.我试图将其子类化并更改contentize的setter以发送NSNotification:

@implementation UIScrollView (Height)

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

@end
Run Code Online (Sandbox Code Playgroud)

但在编译时我得到并且错误说:

"_OBJC_IVAR _ $ _ UIScrollView._contentSize",引自: - MyClass.o中的[UIScrollView(Heigth)setContentSize:] ld:未找到架构armv7的符号

知道如何将setter子类化吗?

谢谢 !

rob*_*off 30

也许您可以使用键值观察(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];
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,您可能需要调整setContentSize:方法.方法调配让您的替换方法调用原始方法,这是将新内容大小传递到滚动视图所需的操作.

您可以在此处阅读有关方法调配的更多信息:http://www.mikeash.com/pyblog/friday-qa-2010-01-29-method-replacement-for-fun-and-profit.html

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