user1741348
user1741348

Reputation: 203

Window Sizing issue

I am creating a minimal WebKit browser for a Mac OS App using the following Swift code to load the new WKWebView into an Custom View Object.

The following line of code creates the web view to fill the size of the customView window:

var theWebView:WKWebView = WKWebView(frame:self.customView.frame)

When you run the App the web page loads correctly to fill the window, however if the window is resized the web page does not resize with the window.

I have added constraints to the Custom View object in interface builder and believe that this object is resizing correctly however it seems that the WKWebView is not adjusting to fill the Custom View?

See attached screen shots below.

Any ideas welcome.

import Cocoa import WebKit

@NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!

@IBOutlet weak var customView: NSView!

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application


    //create a URL for the path
    var url = NSURL(string:"http://www.google.com/")

    //create a request for the URL
    var request = NSURLRequest(URL:url!)

    //create the WKWebView and set the size to match
    //the window's full view
    var theWebView:WKWebView = WKWebView(frame:self.customView.frame)
    //have the web view load the page
    theWebView.loadRequest(request)
    //add the web view to the main window
    self.customView.addSubview(theWebView)
}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application
}   }

before resize

after resize

Upvotes: 1

Views: 1262

Answers (1)

Alex Ryan
Alex Ryan

Reputation: 2866

I had this same issue (in Objective-C) and solved it by putting this one line of code in right after I added webView as a subview.

 webView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

Upvotes: 1

Related Questions