Okay, I had the same problem as you! Check your view's frame on construction and, if it's not starting at 0,0, make sure that the parent view doesn't auto resize the subviews. I tried to keep my code sample to the bare minimum. (note that webConfig.AllowsAirPlayForMediaPlayback isn't required in this sample)
Here's a working example. You can switch the view to be full frame in the window or positioned as a smaller view in the parent. I set the window to be 1024x768 in IB.
You can easily convert this code to Swift.
The key is to not autoresize the subviews if the view is being placed at an offset other than 0,0.
quick sample:
//#define USE_FULL_WINDOW // uncomment this to fit webview full window
using System;
using AppKit;
using Foundation;
using WebKit;
using CoreGraphics;
using System.Diagnostics;
namespace WkWebTest
{
public class MediaView : NSView
{
private WKWebView webView;
public MediaView(CGRect frame)
: base(frame)
{
#if USE_FULL_WINDOW
this.AutoresizesSubviews = true;
this.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
#endif
WKWebViewConfiguration webConfig = new WKWebViewConfiguration();
webConfig.AllowsAirPlayForMediaPlayback = true;
this.webView = new WKWebView(new CGRect(0, 0, frame.Width, frame.Height), webConfig);
this.webView.AutoresizesSubviews = true;
this.webView.AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable;
this.AddSubview(this.webView);
string url = "http://www.apple.com";
Debug.WriteLine("LoadUrl: " + url);
var request = new NSUrlRequest(new NSUrl(url));
this.webView.LoadRequest(request);
}
public override bool IsFlipped { get { return true; } }
}
public partial class ViewController : NSViewController
{
private MediaView mediaView;
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
#if USE_FULL_WINDOW
float mediaViewLeft = 0;
float mediaViewTop = 0;
nfloat mediaViewWidth = View.Frame.Width;
nfloat mediaViewHeight = View.Frame.Height;
#else
float mediaViewLeft = 511;
float mediaViewTop = 112;
nfloat mediaViewWidth = 475;
nfloat mediaViewHeight = 548;
#endif
this.mediaView = new MediaView(new CGRect(mediaViewLeft, mediaViewTop, mediaViewWidth, mediaViewHeight));
this.View.AddSubview(mediaView);
}
// boiler plate code generated by Xamarin
public override NSObject RepresentedObject
{
get
{
return base.RepresentedObject;
}
set
{
base.RepresentedObject = value;
// Update the view, if already loaded.
}
}
}
}