开发者

Cocoa WebView scrollbars won't disappear

I load the webview and set allowsScrolling to NO, but webview still shows scroll bars... Banging your head on your computer hurts a lot more now that MacBooks have sharp metal edges.

My code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
    NSString *webFolder = @"file:///<WebFolderPath>";
    [[[productWeb mainFrame] frameView] setAllowsScrolling:NO];
    [productWeb setFrameLoadDelegate:self];
    [[productWeb mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[webFolder stringByAppendingString:@"webpage.html"]]]];
}

I even setup the frame loading delegate to report about the scrolling status:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
    NSLog(@"Scrolling %@",[[frame frameView] allowsScrolling] ? @"Allowed" : @"Not Allowed");开发者_C百科
    [[frame frameView] setAllowsScrolling:NO];
    NSLog(@"Scrolling %@",[[frame frameView] allowsScrolling] ? @"Allowed" : @"Not Allowed");
}

Which still gives me the unhappy:

2010-08-24 15:20:09.102 myApp[30437:a0f] Scrolling Allowed
2010-08-24 15:20:09.104 myApp[30437:a0f] Scrolling Not Allowed

And yet the scrollbars continue to show! Hopefully, it is something stupid I'm doing as I don't want to get any more blood on my laptop.


I found I had to edit the HTML of the page I was trying to display to make sure that it was setup to take the full screen (and not more)... there was a DIV that had a minimum width set. Once I made sure the body had height = 100% and that none of the DIV's had a fixed or minimum width set that was smaller then the box I wanted to show it in everything came together :)


Are you trying to prevent the user from scrolling the view at all? You can just set productWeb.userInteractionEnabled = NO. Or are you just trying to prevent the bars from showing when the user is scrolling?

Here's another thing you can try: inject some JavaScript into your UIWebView that disables the touchmove event:

[productWeb stringByEvaluatingJavaScriptFromString:@"document.ontouchmove = function(e){e.preventDefault();}"];

This leaves the user interaction enabled, but should prevent any scrolling.


Try this if you use Swift:

import Foundation
import Cocoa
import WebKit


class WebViewRep: WebView {

    //... hidden code

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        //Disable scrolling
        self.mainFrame.frameView.allowsScrolling = false
        //Sets current object as the receiver of delegates
        self.policyDelegate = self
        self.frameLoadDelegate = self
        //Load homepage
        self.loadHomePage()
    }

    //... hidden code

    override func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!) {
        //Completely disable scroll
        frame.frameView.allowsScrolling = false
        self.stringByEvaluatingJavaScriptFromString("document.documentElement.style.overflow = 'hidden';")
    }

}

Sadly, it's not enough to use allowScrolling. It works sometimes, but not always. So, you have to manipulate loaded page too.

Of course, don't forget to set WebViewRep as custom class for your WebView.


I'm guessing your web view is in a nib that's being loaded before -applicationDidFinishLaunching: is called.

A quick fix:

  1. In your nib, select the window and deselect "Visible at Launch" attribute.
  2. Then after you call -setAllowsScrolling:, call -[self.window makeKeyAndOrderFront:nil] to show the window.

A better fix is to move your window loading code from the app delegate to a window controller subclass, do the call to -setAllowsScrolling: in -[NSWindowController windowDidLoad], and call -[NSWindowController showWindow:] from your app delegate to show the window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜