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

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

所以我正在寻找一种方式在scrollview的内容更改时被通知.我试图进行子类化,并改变setter for contensize以发送NSNotification:

@implementation UIScrollView (Height)

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

@end

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

“_OBJC_IVAR_$_UIScrollView._contentSize”,referenced from:
-[UIScrollView(Heigth) setContentSize:] in MyClass.o
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允许您的替换方法调用原始方法,这是您需要做的将新内容大小传递到滚动视图.

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

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

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...