有人知道如何阻止UIWebView垂直反弹吗?我的意思是,当用户触摸他们的iphone屏幕,向下拖动他们的手指时,web视图在我加载的网页上方显示一个空白点?

我研究了以下可能的解决方案,但没有一个适合我:

http://www.iphonedevsdk.com/forum/iphone-sdk-development/996-turn-off-scrolling-bounces-uiwebview.html

http://forums.macrumors.com/showthread.php?t=619534

我如何停止一个UIScrollView水平反弹?


当前回答

在Swift中禁用反弹

webViewObj.scrollView.bounces = false

其他回答

webView.scrollView.scrollEnabled =没有; webView.scrollView.bounces =没有;

我尝试了一种稍微不同的方法来防止UIWebView对象的滚动和反弹:添加一个手势识别器来覆盖其他手势。

看起来,UIWebView或它的scroller子视图使用它自己的平移手势识别器来检测用户滚动。但根据苹果的文档,有一种合法的方法可以覆盖一个手势识别器。UIGestureRecognizerDelegate协议有一个方法gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: -它允许控制任何碰撞手势识别器的行为。

所以,我所做的是

视图控制器的viewDidLoad方法:

// Install a pan gesture recognizer                                                                                        // We ignore all the touches except the first and try to prevent other pan gestures                                                     
// by registering this object as the recognizer's delegate                                                                                        
UIPanGestureRecognizer *recognizer;                                                                                                               
recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)];                                                   
recognizer.delegate = self;                                                                                                                       
recognizer.maximumNumberOfTouches = 1;                                                                                                            
[self.view addGestureRecognizer:recognizer];                                                                                                          
self.panGestureFixer = recognizer;                                                                                                                  
[recognizer release]; 

然后,手势重写方法:

// Control gestures precedence                                                                                                                            
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer                                                                                        
        shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer                                                  
{                                                                                                                                                         
        // Prevent all panning gestures (which do nothing but scroll webViews, something we want to disable in                                          
        // the most painless way)                                                                                                                         
        if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])                                                                        
        {
            // Just disable every other pan gesture recognizer right away                                                                             
            otherGestureRecognizer.enabled = FALSE;
        }                                                                                                                                                  
        return NO;                                                                                                                                        
}              

当然,这个委托方法在实际应用中可能会更复杂——我们可以选择性地禁用其他识别器,分析otherGestureRecognizer。视图,并根据它是什么视图做出决策。

最后,为了完整起见,我们将方法注册为pan处理程序:

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer 
{ 
    // do nothing as of yet
}

如果我们只想取消网页视图的滚动和反弹,它可以是空的,或者它可以包含我们自己的代码来实现我们真正想要的平移运动和动画……

到目前为止,我只是在尝试所有这些东西,它似乎或多或少地像我想要的那样工作。不过,我还没有尝试向iStore提交任何应用程序。但我相信到目前为止我还没有使用任何无证件的东西……如果有人有其他发现,请通知我。

要禁用UIWebView滚动,你可以使用以下代码行:

[ObjWebview setUserInteractionEnabled:FALSE];

在这个例子中,ObjWebview的类型是UIWebView。

警告。我在我的应用程序中使用了setAllowsRubberBanding:,苹果拒绝了它,声明非公共API函数是不允许的(引用:3.3.1)

这里有两个新的潜在解决方案。显然,你可以使用jqtouch或pastrykit禁用滚动。然而,我没有这些工作。你可能会更有能力。

关闭垂直滚动

深入到pastrykit