conradkleinespel
conradkleinespel

Reputation: 7007

Resizing a WebView instance

I'm just starting to learn OSX Cocoa app development. I would like to display a website inside a native OSX window. I thought a WebView would be the right way. I would like the webview to always take up 100% of the containing windows' size.

After struggling a bit, I understand how to catch the 'window resize' event, but I have no clue how to resize the web view according to the windows new size.

Here's what I have so far:

AppDelegate.h

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate, NSWindowDelegate>

@property (assign) IBOutlet NSWindow *window;

@property (weak) IBOutlet WebView *websiteWebview;

@end

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (NSSize) windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
{
    WebView *view = [self websiteWebview];
    [view setFrame:CGRectMake(0, 0, 1000, 1000)];
    return frameSize;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[self window] setDelegate:self];
    NSURL *url = [[NSURL alloc] initWithString:@"http://conradk.com"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    [[[self websiteWebview] mainFrame] loadRequest:request];
}

@end

I thought calling [view setFrame:CGRectMake(0, 0, 1000, 1000)] would resize the web view as well, but it seems to not be the case.

Any tips / hints please? Is a WebView the right way to do this? Thanks for your help!

Upvotes: 2

Views: 3039

Answers (1)

nhgrif
nhgrif

Reputation: 62062

You need to make your WebView part of the window's contentView.

[self.window setContentView:self.websiteWebview];

By default, this will let the webView auto-resize with the window. You'll only need to mess with the sizing if you want the webview to do something other than match the size of the window.

Upvotes: 6

Related Questions