objective-c - 如何使网页 View 的一部分稍微半透明?

标签 objective-c macos cocoa webview alpha-transparency

Objective-CMac OSX Cocoa 中,有没有办法在WebView 稍微半透明,以便窗口后面的桌面应用程序稍微渗透,但仅在该 DIV 上,而不是 WebView 的其余部分?

或者也许我可以使整个 WebView 半透明,但对除一个 DIV 之外的所有 DIV 关闭它?

我有一个正在加载网页的WebView。在网页中,左侧有一个侧边栏,右侧有一个 IFRAME 。我单击侧边栏上的项目,它们会更改右侧的 IFRAME 页面。我想让这个侧边栏变得稍微半透明。

在我的 AppDelegate.m 中,到目前为止我已经有了这个,并且我的 HTMLBODY 标记设置为 背景:无,我的侧边栏设置为background:rgba(0,0,0,0.5),但我最终看到的只是灰色的侧边栏背景。就像 webview 本身想要确保它的背景设置为除透明之外的颜色。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  // note that _window and _wv are outlet properties to my window and webview widgets
  [_window setBackgroundColor:[NSColor clearColor]];
  [_window setOpaque:NO];

  [_wv.layer setBackgroundColor:[NSColor clearColor].CGColor];
  [_wv.layer setOpaque:NO];
}

最佳答案

相关分步教程和示例:

http://stylekit.org/blog/2016/01/23/Chromeless-window/

设置半透明度:

  1. 将 NSWindow 子类的 styleMask 设置为 NSFullSizeContentViewWindowMask (以便半透明效果在标题栏区域中也可见,将其保留,标题栏区域将为空白)
  2. 设置self.titlebarAppearsTransparent = true(隐藏标题栏默认图形)
  3. 将以下代码添加到您的 NSWindow 子类中:(您现在应该有一个半透明的窗口)

.

let visualEffectView = NSVisualEffectView(frame: NSMakeRect(0, 0, 0, 0))//<---the width and height is set to 0, as this doesn't matter. 
visualEffectView.material = NSVisualEffectMaterial.AppearanceBased//Dark,MediumLight,PopOver,UltraDark,AppearanceBased,Titlebar,Menu
visualEffectView.blendingMode = NSVisualEffectBlendingMode.BehindWindow//I think if you set this to WithinWindow you get the effect safari has in its TitleBar. It should have an Opaque background behind it or else it will not work well
visualEffectView.state = NSVisualEffectState.Active//FollowsWindowActiveState,Inactive
self.contentView = visualEffectView/*you can also add the visualEffectView to the contentview, just add some width and height to the visualEffectView, you also need to flip the view if you like to work from TopLeft, do this through subclassing*/

enter image description here

enter image description here

关于objective-c - 如何使网页 View 的一部分稍微半透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34504699/

相关文章:

objective-c - Cocoa/Objective-C(已弃用 FMActivateFonts)

ios - 为什么这个 UIViewController 方法调配不起作用?

ios - 重新排序表行 SQLite UITableView

swift - 更改 NSVisualEffectView 的颜色

c# - 我不明白 PlatformID 枚举

objective-c - AudioUnitInitialize 返回 -10851 (kAudioUnitErr_InvalidPropertyValue)

objective-c - 权限被拒绝,由 NSURL 资源泄漏引起?

ios - 如何将过滤器应用于以前录制的声音并使用 AudioKit 保存修改后的版本?

ios - 关于 UIGestures 的困惑 - 消息接收者

c++ - Ifstream 在 OSX 上看不到新内容