How do I embed a WebView in an NSScrollView?
So I have a project that has some content being displayed in a WebView and 开发者_开发知识库I want to stick above that. I want the header to scroll with the WebView content. Now, WebView generally wants to do it's own scroll handling but you can tell it not to using:
[[webView mainFrame] setAllowsScrolling:NO];
which successfully makes the WebView's scroll bars not appear. However, despite the fact that it's embedded in an NSScrollView, the NSScrollView's scroll bars never activate. I can only conclude that the WebView needs to be told to resize, but I can't figure out how to do that. I have tried the following:
NSRect webViewBounds = [webView bounds];
[webView setFrameSize:webViewBounds.size];
but that doesn't appear to work either.
Any suggestions?
Alternative solution: What about expressing the header in HTML/CSS/JavaScript, and putting the real content in an iframe? You can traverse the frame hierarchy to find the WebFrame object for that frame, and use the DOM (perhaps wrapped by an object of your own) to control the header.
I had a quick look at the docs for this, probably a silly question; after you change the frame size are you sending a setNeedsDisplay
message to the view?
The bounds and frame in your code should have the same size. Setting one to the other changes nothing.
Today I played a little bit with the WebView class. With my approach you wouldn’t have to add your header to the HTML code. Registering for a notification gives you a starting point:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(webViewDidScroll:)
name:NSViewBoundsDidChangeNotification object:nil];
Then you can check the class of the notification sender (let’s assume you have only one WebView):
- (void)webViewDidScroll:(NSNotification *)aNotification
{
if ([[aNotification object] isKindOfClass:NSClassFromString(@"WebClipView")])
{
// Do whatever you like; scroll your header etc.
}
}
精彩评论