如何检测 UIWebView 到达顶部或底部? 因为我需要在到达底部时触发一个 Action 。
那可行吗?
最佳答案
首先,如 UIWebView
文档中所述,您不应将 UIWebView
放在 UIScrollView
中。
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
相反,您可以通过 UIWebView
属性访问 UIScrollView
。您的 View Controller 可以实现 UIScrollViewDelegate
。
@interface MyViewController : UIViewController<UIScrollViewDelegate>
@end
然后你可以 Hook 到 UIScrollView:
- (void)viewDidLoad
{
[super viewDidLoad];
// Set self as scroll view protocol
webView.scrollView.delegate = self;
}
然后最终检测到达顶部和底部:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if(scrollView.contentOffset.y
>=
(scrollView.contentSize.height - scrollView.frame.size.height)){
NSLog(@"BOTTOM REACHED");
}
if(scrollView.contentOffset.y <= 0.0){
NSLog(@"TOP REACHED");
}
}
瞧,您可以检测何时到达顶部和底部。
关于iphone - iOS : detect UIWebView reaching the top or bottom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9535303/