我正在为 iOS 6 进行开发,并且我正在开发 UIWebView 顶部的一种工具栏,当您在 UIWebView 上向上滚动时,该工具栏会后退。
我的想法是,我可以设置 UIWebView 内的 UIScrollView 的委托(delegate),以向我的 UIViewController 发送消息,这将为顶部工具栏上的约束设置动画,使其收缩和移动等等。
但是,scrollViewDidScroll:
方法似乎没有被调用,这让我感到困惑。
现在,关于 SO 的其他问题还有许多与此类似的问题,其中大部分似乎是人们忘记在 scrollView 上设置委托(delegate)属性...
不用说,我正在设置委托(delegate)。一些代码片段:
SDDefinitionViewController.h:
@interface SDDefinitionViewController : UIViewController <UIWebViewDelegate, UIScrollViewDelegate>
...
SDDefinitionViewController.m:
...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadDefinitions];
// Grab a reference to the ScrollView so that we can set the delegate and then use those method calls to animate the top bar.
self.scrollView = self.webView.scrollView;
self.scrollView.delegate = self;
self.lastScrollViewVerticalOffset = 0;
}
...
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// (Code) <= Never gets called
}
...
而且,最重要的是,我做了一些调试器控制台操作:
(lldb) po self
$0 = 0x095c0600 <SDDefinitionViewController: 0x95c0600>
(lldb) po self.scrollView
$1 = 0x0954e8d0 <_UIWebViewScrollView: 0x954e8d0; frame = (0 0; 1024 596); clipsToBounds = YES; autoresize = H; gestureRecognizers = <NSArray: 0x954ec50>; layer = <CALayer: 0x954eab0>; contentOffset: {0, 0}>
(lldb) po self.scrollView.delegate
$2 = 0x095c0600 <SDDefinitionViewController: 0x95c0600>
所以,委托(delegate)肯定设置正确,没有任何东西被随机释放。
最后,我以编程方式调用了
setContentOffset
在 self.scrollView
并且委托(delegate)方法确实被调用了。但这并没有真正的帮助,因为我想检测用户何时使用手势滚动。那么,这有什么独家新闻呢?为什么 scrollView 不会调用它的委托(delegate)方法?
编辑:
所以,用 Reveal 查看我的应用程序,我看到我的 UIWebView 有一个 _UIWebViewScrollView 作为 subview ,在它下面有一个 UIWebBrowserView。 UIWebBrowserView 的大小与 UIWebView 完全相同...
难道是 UIWebViewScrollView 没有一个正常的内容 View ,它会偏移等等,webkit 正在做滚动还是什么?
最佳答案
如UIWebView
内部实现 UIScrollViewDelegate
方法。 UIScrollViewDelegate
不太可能将被重新分配给除当前 UIWebView 对象之外的其他对象。
所以,我找到了一个小工作。你可以继承 UIWebView 实现 UIScrollViewDelegate
该类中的方法,并且不要忘记将该方法转发给 super ,否则可能会发生意外行为。
这就是我解决上述问题的方法:
/////in WebView.h file
//////WebView is subclass of UIWebView
#import <UIKit/UIKit.h>
@interface WebView : UIWebView
@property(nonatomic,weak) NSObject <UIScrollViewDelegate> *mScrollDelegate;
@end
//// in WebView.m file
#import "WebView.h"
@synthesize mScrollDelegate;
@implementation WebView
- (id)init
{
self = [super init];
if (self) {
self.scrollView.delegate = self;
}
return self;
}
/////scrollViewDidScroll Method, Also I am forwarding it to super class UIWebView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[super scrollViewDidScroll:scrollView];
if([self.mScrollDelegate respondsToSelector:@selector(scrollViewDidScroll:)]){
[self.mScrollDelegate scrollViewDidScroll:scrollView];
}
NSLog(@"scrollViewDidScroll");
}
@end
并确保您的
self.webView
必须是 WebView
类型.在 SDDefinitionViewController.m
self.webView.mScrollDelegate = self;
现在 SDDefinitionViewController.m 中的 scrollViewDidScroll 方法将被调用
关于ios - UIWebView scrollView 没有在触摸时调用 scrollViewDidScroll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17253345/