gpt4 book ai didi

objective-c - 如何使用 Cocoa 创建一个有阴影且没有奇怪标题栏的模态表?

转载 作者:行者123 更新时间:2023-12-03 17:47:42 26 4
gpt4 key购买 nike

我第一次尝试使用Cocoa Sheet,但无法达到我想要的效果。因此,一般来说,我想要一个模态表,它可以阻止底层窗口的所有输入,并具有标准窗口阴影并且没有明确的标题栏。这是我到目前为止所得到的:

enter image description here

我通过以下代码实现了这一点:

- (BOOL)windowShouldClose:(id)sender
{
NSRect sheetFrame = NSMakeRect(0, 0, 432, 224);
NSWindow* window = [[NSWindow alloc] initWithContentRect:sheetFrame styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO];
[window setHasShadow:YES];
window.backgroundColor = [NSColor redColor];
window.titlebarAppearsTransparent = true;
QuitView* quitView = [[QuitView alloc] initWithFrame:sheetFrame];
window.contentView = quitView;
__block SheetController* sheetController = [[SheetController alloc] initWithWindow:_window andSheet:window];
[quitView.noButton setTarget:sheetController];
[quitView.noButton setAction:@selector(quitSheetAndContinue)];
[quitView.quitButton setTarget:sheetController];
[quitView.quitButton setAction:@selector(quitSheetAndAbort)];
[_window beginSheet:window completionHandler:^(NSModalResponse responseCode)
{
sheetController = nil;
if (responseCode == NSModalResponseAbort)
{
[self quit];
}
}];
return NO;
}

- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet usingRect:(NSRect)rect
{
return NSMakeRect(144, 336, 432, 0);
}

我想解决的问题是:1)删除纸张顶部奇怪的灰线(谁能解释它是从哪里来的?)。2)即使按钮不可点击,底层窗口仍然接收悬停事件,我可以禁用它吗?3)我希望工作表具有标准窗口阴影,如代码片段所示,我尝试显式启用它,但没有成功。

任何人都可以就上述部分/全部 3 点提供帮助/提示吗?

最佳答案

这是在 Swift 和 Xcode 9.1 中使用 Storyboard,

class ViewController: NSViewController {

var viewController: NSViewController?

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
viewController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(rawValue: "MyModalViewController")) as? NSViewController
}

override func viewWillAppear() {
//self.presentViewControllerAsSheet(viewController!)
self.presentViewControllerAsModalWindow(viewController!)

self.view.window?.center()
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}

}




class MyModalViewController: NSViewController {

@IBAction func buttonPressed(_ sender: NSButton) {
dismissViewController(self)
}

override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}

override func viewWillAppear() {

self.view.window?.titleVisibility = .hidden
self.view.window?.titlebarAppearsTransparent = true

self.view.window?.styleMask.insert(.fullSizeContentView)

self.view.window?.styleMask.remove(.closable)
self.view.window?.styleMask.remove(.fullScreen)
self.view.window?.styleMask.remove(.miniaturizable)
self.view.window?.styleMask.remove(.resizable)

self.view.window?.isMovable = false
}

}

Output

您也可以为主窗口实现所需的样式掩码。主窗口是可移动的,但放置 self.view.window?.isMovable = false到 ViewController viewWillAppear() 以禁用移动。

在MyModalViewController中,在dismissViewController(self)之前,可以使用let vc =presenting as!来启用它! ViewControllervc.view.window?.isMovable = true 在关闭 Controller 之前。

关于objective-c - 如何使用 Cocoa 创建一个有阴影且没有奇怪标题栏的模态表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46116979/

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