我一直在网上搜索,但我还没有遇到更改 HUD 透明度的方法(所有面板透明度,包括标题栏)。有可能改变它吗?
谢谢
您应该能够使用setAlphaValue
继承自 NSWindow 的 ,:
[ myPanel setAlphaValue: 0.5 ];
有可能的。您只需要通过在面板视图的核心动画层上设置颜色(包括 alpha)以编程方式执行此操作。下面是一个将 HUD 面板设置为透明度稍低的示例:
view.layer.backgroundColor = [NSColor colorWithSRGBRed:0.2 green: 0.2 blue: 0.2 alpha:0.7].CGColor
请记住,您必须在视图加载后执行此操作,例如。不要在init中设置它:
It's not possible. HUD panels are intended to be transparent; they won't let you change their opacity or the opacity of their base views.
NSLog(@"opaque before? %@", [hud isOpaque] ? @"YES" : @"NO");
[hud setOpaque:YES];
NSLog(@"opaque after? %@", [hud isOpaque] ? @"YES" : @"NO");
OpaqueHUD[18952:a0b] opaque before? NO
OpaqueHUD[18952:a0b] opaque after? NO
NSLog(@"alpha before: %.2f", [hud alphaValue]);
[hud setAlphaValue:1.0f];
NSLog(@"alpha after: %.2f", [hud alphaValue]);
OpaqueHUD[18952:a0b] alpha before: 1.00
OpaqueHUD[18952:a0b] alpha after: 1.00
NSView * contentView = [hud contentView];
// In layer-backed mode
NSLog(@"content alpha before: %.2f", [contentView alphaValue]);
[contentView setAlphaValue:1.0];
NSLog(@"content alpha after: %.2f", [contentView alphaValue]);
OpaqueHUD[18952:a0b] content alpha before: 1.00
OpaqueHUD[18952:a0b] content alpha after: 1.00
You'll have to: 1) put a custom opaque subview in there and live with a translucent title bar; b) use an NSPanel
with the regular style, whose background color and opacity you can change, and live with it being a regular title bar; or d) create your very own custom window (good link to another writeup at the bottom of that article). See also this article about making your own window frame (warning: that one uses private API, and is a few years old).