問題描述
我正在嘗試在 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 的變化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!