代码之家  ›  专栏  ›  技术社区  ›  BadPirate

Cocoa WebView滚动条不会消失

  •  6
  • BadPirate  · 技术社区  · 14 年前

    我加载webview并将allowscrolling设置为no,但是webview仍然显示滚动条…既然MacBook有锋利的金属边缘,在电脑上敲打你的头会更疼。

    我的代码:

    - (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"]]]];
    }
    

    我甚至设置帧加载代理来报告滚动状态:

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

    这仍然让我不高兴:

    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
    

    但是滚动条继续显示!希望,这是一件愚蠢的事情,因为我不想在我的笔记本上得到更多的血液。

    4 回复  |  直到 10 年前
        1
  •  1
  •   BadPirate    14 年前

    我发现我必须编辑我试图显示的页面的HTML,以确保它设置为全屏显示(而不是更多)。有一个DIV设置了最小宽度。一旦我确定了身体的高度为100%,并且没有一个师有一个固定的或最小的宽度集比我想显示的盒子小,所有的东西都在一起:)

        2
  •  0
  •   Don    14 年前

    您是否试图阻止用户滚动视图?你可以设定 productWeb.userInteractionEnabled = NO . 或者你只是想阻止用户滚动时显示条?

    还有一件事你可以尝试:在你的uiwebview中注入一些javascript来禁用touchmove事件:

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

    这将使用户交互保持启用状态,但应防止任何滚动。

        3
  •  0
  •   D.A.H    10 年前

    如果使用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';")
        }
    
    }
    

    遗憾的是,使用allowscrolling还不够。它有时有效,但不总是有效。所以,您也必须操作加载的页面。

    当然,不要忘记将WebViewRep设置为WebView的自定义类。

        4
  •  -1
  •   jrc    12 年前

    我猜你的网络视图在一个正在加载的NIB中 之前 -applicationDidFinishLaunching: 被称为。

    快速修复:

    1. 在您的NIB中,选择窗口并取消选择“启动时可见”属性。
    2. 然后在你打电话之后 -setAllowsScrolling: ,呼叫 -[self.window makeKeyAndOrderFront:nil] 显示窗口。

    一个更好的解决方法是将窗口加载代码从应用程序委托移动到窗口控制器子类,执行调用 -设置允许滚动: 在里面 -[NSWindowController windowDidLoad] 和呼叫 -[NSWindowController showWindow:] 从应用程序代理中显示窗口。