1

我正在使用以下 Swift 代码为 Mac OS 应用程序创建一个最小的 WebKit 浏览器,以将新的 WKWebView 加载到自定义视图对象中。

以下代码行创建 Web 视图以填充 customView 窗口的大小:

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

当您运行应用程序时,网页会正确加载以填充窗口,但是如果调整窗口大小,则网页不会随窗口调整大小。

我在界面生成器中为自定义视图对象添加了约束,并相信该对象正在正确调整大小,但似乎 WKWebView 没有调整以填充自定义视图?

请参阅下面的附加屏幕截图。

欢迎任何想法。

导入 Cocoa 导入 WebKit

@NSApplicationMain 类 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
}   }

调整大小之前

调整大小后

4

1 回答 1

1

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;
于 2015-01-29T03:44:16.943 回答