Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a rounded-corner WKWebView on OS X?

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.

like image 479
Xhacker Liu Avatar asked Dec 19 '22 07:12

Xhacker Liu


1 Answers

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"]]];
}
like image 179
Petesh Avatar answered Jan 12 '23 23:01

Petesh