2014-10-21 69 views
1

我正在使用以下Swift代碼將新的WKWebView加載到自定義視圖對象中,爲Mac OS App創建最小的WebKit瀏覽器。窗口大小問題

下面的代碼行創建web視圖以填充customView窗口的大小:

VAR theWebView:WKWebView = WKWebView(幀:self.customView.frame)

當你運行應用程序的網頁加載正確,以填補窗口,但是如果窗口調整大小的網頁不調整窗口。

我已經在界面生成器中的自定義視圖對象中添加了約束,並相信此對象正確調整大小,但似乎WKWebView未調整以填充自定義視圖?

請參閱下面的附加屏幕截圖。

任何想法歡迎。

進口可可進口的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 
} } 

before resize

after resize

回答

1

我有這個相同的問題(在Objective-C中),並且通過在我將webView作爲子視圖添加後正好放入這一行代碼來解決它。

webView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;