gpt4 book ai didi

objective-c - 带圆角和阴影的 NSWindow

转载 作者:太空狗 更新时间:2023-10-30 03:14:22 24 4
gpt4 key购买 nike

我正在尝试创建一个没有标题栏(NSBorderlessWindowMask)的带有圆角和阴影的NSWindow,类似于下面的“欢迎使用 Xcode”窗口。

Welcome to Xcode

我创建了一个 NSWindow 的子类:

@implementation FlatWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag];

if ( self )
{
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setMovableByWindowBackground:TRUE];
[self setStyleMask:NSBorderlessWindowMask];
[self setHasShadow:YES];
}

return self;
}

- (void) setContentView:(NSView *)aView
{
aView.wantsLayer = YES;
aView.layer.frame = aView.frame;
aView.layer.cornerRadius = 10.0;
aView.layer.masksToBounds = YES;

[super setContentView:aView];
}

@end

NSView 的子类:

@implementation ColoredView

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

[[NSColor windowBackgroundColor] set];
NSRectFill(dirtyRect);
}

@end

这给了我一个没有标题栏的圆角窗口,但是 NSWindow 上的默认阴影消失了。如何将默认阴影添加到此窗口?

Flat window

编辑 1:

带有 NSShadow 的 NSWindow。此阴影未显示。

@implementation FlatWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag];

if ( self )
{
[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];
[self setMovableByWindowBackground:TRUE];
[self setStyleMask:NSBorderlessWindowMask];
[self setHasShadow:YES];
}

return self;
}

- (void) setContentView:(NSView *)aView
{
aView.wantsLayer = YES;
aView.layer.frame = aView.frame;
aView.layer.cornerRadius = 10.0;
aView.layer.masksToBounds = YES;

NSShadow *dropShadow = [[NSShadow alloc] init];
[dropShadow setShadowColor:[NSColor blackColor]];
[dropShadow setShadowBlurRadius:10.0];
[aView setShadow: dropShadow];

[super setContentView:aView];
}

@end

最佳答案

更新

我意识到旧方法无法创建精确的圆角。所以我更新了示例以制作精确的圆角。

enter image description here

        window1.backgroundColor             =   NSColor.whiteColor()
window1.opaque = false
window1.styleMask = NSResizableWindowMask
| NSTitledWindowMask
| NSFullSizeContentViewWindowMask
window1.movableByWindowBackground = true
window1.titlebarAppearsTransparent = true
window1.titleVisibility = .Hidden
window1.showsToolbarButton = false
window1.standardWindowButton(NSWindowButton.FullScreenButton)?.hidden = true
window1.standardWindowButton(NSWindowButton.MiniaturizeButton)?.hidden = true
window1.standardWindowButton(NSWindowButton.CloseButton)?.hidden = true
window1.standardWindowButton(NSWindowButton.ZoomButton)?.hidden = true

window1.setFrame(CGRect(x: 400, y: 0, width: 400, height: 500), display: true)
window1.makeKeyAndOrderFront(self)

Here的完整工作示例。


过时

至少在 OS X 10.10 中不需要特殊处理。

import Cocoa

class ExampleApplicationController: NSObject, NSApplicationDelegate {
class ExampleController {

let window1 = NSWindow()
let view1 = NSView()

init(){
window1.setFrame(CGRect(x: 400, y: 0, width: 400, height: 500), display: true)
window1.contentView = view1

window1.backgroundColor = NSColor.clearColor()
window1.opaque = false
window1.styleMask = NSBorderlessWindowMask | NSResizableWindowMask
window1.movableByWindowBackground = true
window1.makeKeyAndOrderFront(self)

view1.wantsLayer = true
view1.layer!.cornerRadius = 10
view1.layer!.backgroundColor = NSColor.whiteColor().CGColor

/// :ref: http://stackoverflow.com/questions/19940019/nswindow-with-round-corners-and-shadow/27613308#21247949
window1.invalidateShadow() // This manual invalidation is REQUIRED because shadow generation is an expensive operation.
}
}

let example1 = ExampleController()
}

您可以从 here 下载工作示例.

关于objective-c - 带圆角和阴影的 NSWindow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19940019/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com