objective-c - 带有 Cordova 的 OSX 上的圆形 WebView?

标签 objective-c macos cordova webview

我正在使用 Cordova-osx 创建一个 Mac OSX 应用程序.我已经尝试实现我所见过的关于为我的应用程序设置圆角的所有解决方案,但它们都不起作用。最后一个资源是移除阴影并使用 CSS 创建角,但我不太喜欢这种方法。

所以,this is the file负责创建 webview 并加载它。这是我的方法:

- (void) awakeFromNib
{
    _commandDelegate = [[CDVCommandDelegateImpl alloc] initWithViewController:self];
    self.webViewDelegate.viewController = self;

    NSURL* appURL = nil;
    NSString* loadErr = nil;

    if ([self.startPage rangeOfString:@"://"].location != NSNotFound) {
        appURL = [NSURL URLWithString:self.startPage];
    } else if ([self.wwwFolderName rangeOfString:@"://"].location != NSNotFound) {
        appURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", self.wwwFolderName, self.startPage]];
    } else {
        NSString* startFilePath = [self.commandDelegate pathForResource:self.startPage];
        if (startFilePath == nil) {
            loadErr = [NSString stringWithFormat:@"ERROR: Start Page at '%@/%@' was not found.", self.wwwFolderName, self.startPage];
            NSLog(@"%@", loadErr);
            self.loadFromString = YES;
            appURL = nil;
        } else {
            appURL = [NSURL fileURLWithPath:startFilePath];
        }
    }

    if (!loadErr) {
        NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
        self.webView.layer.cornerRadius = 10;
        self.webView.layer.opaque = NO;

        [[self.webView mainFrame] loadRequest:appReq];

    } else {
        NSString* html = [NSString stringWithFormat:@"<html><body> %@ </body></html>", loadErr];
        [[self.webView mainFrame] loadHTMLString:html baseURL:nil];
    }

    for (NSString* pluginName in self.startupPluginNames) {
        [self getCommandInstance:pluginName];
    }

    // initialize items based on settings

    BOOL enableWebGL = [[self.settings objectForKey:@"EnableWebGL"] boolValue];
    WebPreferences* prefs = [self.webView preferences];

    // Note that this preference may not be Mac App Store safe
    if (enableWebGL && [prefs respondsToSelector:@selector(setWebGLEnabled:)]) {
        [prefs performSelector:@selector(setWebGLEnabled:) withObject:[NSNumber numberWithBool:enableWebGL]];
    }
}

基本上是添加这个:

self.webView.layer.cornerRadius = 10;
self.webView.layer.opaque = NO;

这是我用来显示面板的代码:

- (void) showPanel
{

    NSPoint mouseLocation = [NSEvent mouseLocation];

    NSEnumerator *screenEnumerator = [[NSScreen screens] objectEnumerator];
    NSScreen *screen;
    while ((screen = [screenEnumerator nextObject]) && !NSMouseInRect(mouseLocation, screen.frame, NO))
        ;

    NSRect statusFrame = [[self.statusItem valueForKey:@"window"] frame];
    NSRect winFrame = [self.window frame];
    NSRect screenFrame = [screen frame];

    NSPoint p = NSMakePoint(statusFrame.origin.x, screenFrame.size.height + screenFrame.origin.y - 32);


    if ((p.x + winFrame.size.width) > (screenFrame.origin.x + screenFrame.size.width)) {
        p.x = screenFrame.origin.x + screenFrame.size.width - winFrame.size.width - 30;
    }

    [self.window setFrameTopLeftPoint:p];
    [self.window setIsVisible:YES];
    [NSApp activateIgnoringOtherApps:YES];
    [self.window makeKeyAndOrderFront:self];
}

但框架中似乎没有显示任何圆角。有什么想法吗?

最佳答案

我还是不能做出合适的 View ,因为有白角,但是我把WebView的角弄圆了。我只是在 applicationDidFinishLaunching 方法中写的:

self.webView.wantsLayer = YES;
self.webView.layer.cornerRadius = 50.0f;
self.webView.layer.masksToBounds = YES;
[self.webView.mainFrame.frameView setAllowsScrolling:NO];

enter image description here

如你所见,四舍五入是可行的,但角落里也有白色的地方,我不知道如何处理它们

关于objective-c - 带有 Cordova 的 OSX 上的圆形 WebView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21653058/

相关文章:

c++ - 分配 'half' 类型时出现 OpenEXR 链接器错误

javascript - 在android中将文件作为图像文件写入sd卡

javascript - AngularJS http.post() 返回 404

iphone - 何时对 NSMutableArray 使用 mutableCopy

ios - 我想在 FSCalendarVIew 中选择 DateCell 的位置。我怎么能得到?

swift - 使用 swift 获取 OS X 中 .app 包的绝对路径

javascript - 将 Cordova 应用程序移植到移动 Chrome 应用程序的原因?

iphone - 关于 iPhone 上的 ivars 和 @property 的问题

iphone - 使用 AirDrop 发送和接收文件

macos - 如何加速以下 sed 命令