How to make a rounded-corner WKWebView on OS X?
Previously with NSWebView, we can do it through layer:
webView.wantsLayer = YES;
webView.layer.cornerRadius = 5;
webView.layer.masksToBounds = YES;
But on WKWebView it no longer works.
layers don't perform implicit masking of the detail underneath it. To get the corner to mask, you should add the following to the code:
webView.layer.masksToBounds = YES;
From the documentation of CALayer
:
When the value of this property is
YES
, Core Animation creates an implicit clipping mask that matches the bounds of the layer and includes any corner radius effects.The default value of this property is
NO
.
I used the following simple, example code, and it displays the rounded corners:
- (void)windowDidLoad {
[super windowDidLoad];
WKWebView *wv = [[WKWebView alloc] initWithFrame:self.containerView.bounds
configuration:[[WKWebViewConfiguration alloc] init]];
wv.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[self.containerView addSubview:wv];
wv.wantsLayer = YES;
wv.layer.cornerRadius = 50;
wv.layer.masksToBounds = YES;
[wv loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://stackoverflow.com"]]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With