Show and hide window instead of terminating app on close click in cocoa app
I have found solution. Thanks to @Silvester suggestion to present NSViewController.
On button click event :
@IBAction func onButtonClick(_ sender: VSButton) {
let animator = ReplacePresentationAnimator()
let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
present(vc, animator: animator)
}
Custom animator class :
class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {
func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = fromViewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
fromViewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
viewController.view.alphaValue = 0
window.contentViewController = viewController
viewController.view.animator().alphaValue = 1.0
})
}
}
func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = viewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
viewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
fromViewController.view.alphaValue = 0
window.contentViewController = fromViewController
fromViewController.view.animator().alphaValue = 1.0
})
}
}
}
This will work perfectly with Silvester MyWindowController. Thank you.
OS X design decisions. Terminate the app on last window close?
Per Apple's Human Interface Guidelines (a guide for Mac developers):
In most cases, applications that are
not document-based should quit when
the main window is closed. For
Example, System Preferences quits if
the user closes the window. If an
application continues to perform some
function when the main window is
closed, however, it may be appropriate
to leave it running when the main
window is closed. For example, iTunes
continues to play when the user closes
the main window.
macCatalyst app: how to close a window without terminating the app?
I've discussed the problem with Apple technical developer support. After a long discussion, they accepted that this is a bug on Apple side. It's escalated to the engineering team.
UPDATE:
Apple Engineering team has provided the following information regarding this issue and it worked for me:
We were able to prevent the quitting behavior by adding NSSupportsAutomaticTermination = NO to info.plist
Closing frontmost window in Cocoa in an app without a menu bar
If you put a File > Close menu item in your MainMenu nib, the shortcut will work, even though the menu isn't visible.
If you choose to implement an app-wide listener for the shortcut instead, you can get rid of the beep by returning nil from the block, so that the original event doesn't get passed on.
Cocoa : Hide any window using accessibility
You can't do that to a window using Accessibility. You can set the AXHidden of an AXApplication, or the AXMinimized of an AXWindow, but you can't simply hide (set AXHidden of) an AXWindow.
How to hide window of UIAgent process with cocoa
I solved it with NSDistributionNotifications
. In the UIAgent application I add an observer to a @"QuitProcessNotification"
(any other name):
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self selector:@selector(quit:)
name:@"QuitProcessNotification"
object:@"com.MyCompany.MyApp"
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
The callback looks like that:
- (void) quit:(NSNotification *) notification
{
[NSApp terminate:nil];
}
In the main application:
Sending notification:
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:@"QuitProcessNotification"
object:@"com.MyCompany.MyApp"
userInfo: nil /* no dictionary */
deliverImmediately: YES];
Be sure, that the object
parameter is indeed your sender application's bundle identifier.
Related Topics
How to Draw a Line Between Two Points Over an Image in Swift 3
Shorthand for Wrapping a Swift Variable in an Optional
Passing in Variable Number of Args from One Function to Another in Swift
Issue with Returning a Directory Enumerator from Nsfilemanager Using Enumeratoraturl in Swift
Swift Display Image UIimageview
Difference Between Sort and Sortinplace in Swift 2
Override UIgesturerecognizer Touchesbegan
Conversion Between Cgfloat and Nsnumber Without Unnecessary Promotion to Double
Bad_Access During Recursive Calls in Swift
How to Retrieve The Name of Participants in Msconversation
How to Cast from Cftyperef to Axuielement in Swift
Swift Protocol Extension Implementing Another Protocol with Shared Associated Type
Swift: Binary Operator '==' Cannot Be Applied to Operands of Type "Protocol"
Why Would One Use Nested Classes
Iocreateplugininterfaceforservice Returns Mysterious Error
How to Get The Coordinates of The Point on a Line That Has The Smallest Distance from Another Point
How to Print Http Request to Console
Is There a Way in Swiftui to Detect If a User Has Larger Text Size Enabled