检测 UIWebView 的滚动视图 contentSize 的变化

detect changes to UIWebView#39;s scroll view#39;s contentSize(检测 UIWebView 的滚动视图 contentSize 的变化)
本文介绍了检测 UIWebView 的滚动视图 contentSize 的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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...

所以我正在寻找一种在滚动视图的内容大小发生变化时得到通知的方法.我尝试将其子类化并更改 contentsize 的设置器以发送 NSNotification:

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",引用自:-[UIScrollView(Heigth) setContentSize:] 在 MyClass.old: 未找到架构 armv7 的符号

"_OBJC_IVAR_$_UIScrollView._contentSize", referenced from: -[UIScrollView(Heigth) setContentSize:] in MyClass.o ld: symbol(s) not found for architecture armv7

知道 setter 应该如何子类化吗?

Any idea how the setter should be subclassed ?

谢谢!

推荐答案

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

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:

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: 方法.方法调配让您的替换方法调用原始方法,这是您将新的内容大小传递给滚动视图所需的操作.

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.

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

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

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

I think this is the most popular code for swizzling: https://github.com/rentzsch/jrswizzle

这篇关于检测 UIWebView 的滚动视图 contentSize 的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定义、交互式)视图控制器呈现和解除处理滚动视图)