iphone - iOS : detect UIWebView reaching the top or bottom

标签 iphone ios uiwebview

如何检测 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/

相关文章:

iphone - 如何在 Objective-C 中制作正则表达式

iphone - Objective-C 错误 : [UIView copyWithZone:]: unrecognized selector sent to instance

ios - EXC_BAD_ACCESS 在使用 UIWebView 对 ViewController 执行 segue 时?

android - cordova 上的 css 问题(li 的底部边框宽度不同)?

iphone - 如何在应用程序中录制时创建红色标题

iphone - 需要一些关于 Apple 开发程序的信息

ios - 是否可以通过编程方式关闭 iOS 中的拨号辅助功能?

ios - 如何在 UIButton 上只添加顶部边框?

iOS:在 UIView 中动态创建 UISegmentedControl

iphone - 如何向 UIWebView 子类添加手势识别器?