NSWindow close button is disabled - cocoa

Goal: window close button to be present at all times..
I tried to use
override func viewDidAppear() {
var button = view.window?.standardWindowButton(NSWindowButton.CloseButton)
button?.enabled = true
}
But when i move from one view controller to the other, the close window button is still disabled. Note that the view controllers are presented as sheets..
How can i have the button enabled in all view controllers?

Related

SwiftUI ColorPicker Fails In Background (Menubar) App

Context
I have an app that runs only from the macOS menubar. (The LSUIElement property in info.plist is set to YES).
Instead of a menu, this app shows an NSPopover when the menubar button is clicked. The popover holds an NSHostingView which has an extremely simple SwiftUI view:
struct PopoverContentView: View
{
#State private var color: CGColor = .white
var body: some View
{
ColorPicker(selection: $color) {
Text("Pick a Color:")
}
}
}
Problem
Clicking on the ColorPicker() does not open the macOS color picker window. The UI of the ColorPicker() button changes, to show the "selected" border state but the color-picker window never appears.
However, if I change LSUIElement to be NO and then make the app active by clicking its Dock icon (so that it takes over the menubar), THEN clicking on the ColorPicker() in the popover actually reveals the color-picker window.
Do you know of a way to force macOS to show the color-picker window for a background application?
The answer turned out to be simple. In the AppKit ViewController that opens the popover when the menubar button is clicked (PopoverController, for me), I simply did this:
extension PopoverController: NSPopoverDelegate
{
func popoverWillShow(_ notification: Notification)
{
NSApp.activate(ignoringOtherApps: true)
}
}
The ColorPicker now correctly shows the standard macOS system color panel on click.

How do I reliably get windowDidBecomeMain and windowDidResignMain events?

I have a view controller in which I want to receive windowDidBecomeMain and windowDidResignMain events.
In viewWillApear() I set the window delegate to self.
view.window?.delegate = self
I have added an extension to my view controller that conforms to NSWindowDelegate and have implemented both methods in it thus:
extension CustomerListViewController: NSWindowDelegate
{
func windowDidBecomeMain(_ notification: Notification)
{
print("Customer list did become main")
}
func windowDidResignMain(_ notification: Notification)
{
print("Customer list did resign main")
}
}
This is not the initial window. It is opened via a menu item with a Window controller show segue.
When the window is first opened via the menu item, it does not receive windowDidBecomeMain.
When I click another window it does receive windowDidResignMain.
If I then click back into the newly opened window it does receive windowDidBecomeMain and will from that point on.
I suspect that I need to set my window delegate at a different point but don't have a clue where to do so, if that is the case.

Can we override NAVIGATION BACK BUTTON press in Xamarin.forms?

Can we override navigation back button pressed in Xamarin.forms?
I have one navigation back button and the one save button in navigation bar.Save button hits the web service and saves in asynchronous way. While saving although i used progressing bar, navigation back button can be pressed and hence the app crashes due to index out of range exception on navigation stack.I tried using OnDisappearing() , did not work. I wanna cancel the PopUpAsync(),if the save is not done completely, but failed to achieve that. Is there any solution for this scenario? Can we override the navigation back button press event using any custom renderer ?
For controlling the back button to do what I want, I used this method in Xamarin:
public override bool OnKeyDown(Keycode HWkeyCode, KeyEvent e)
{
if (HWkeyCode == Keycode.Back)
{
StartActivity(typeof(FrontPageActivity));
return true;
}
return false;
}

Why doesn't button state remain?

I have the following code:
#IBAction func mybuttonclick(sender: UIButton) {
if(sender.titleLabel?.text == "Start"){
sender.titleLabel?.text = "Change"
}
else {
sender.titleLabel?.text = "Start"
}
}
When I click the button, I see "Change" flash and then it goes back to "Start". This is a simple new test app. The above is the only code I have in the app. Why does the button text change back to "Start" instead of remaining on "Change"?
Its very hard to answer this question without more information, but I think it's possible you have the action bound twice in Interface Builder. Where the action is being fired twice each time you hit the button. Another possibility is you have the button inside a table or collection view cell where the cell is being reused and replacing your previously edited button.

How can I keep my window always zoomed?

So I have storyboard with 2 view controller's and 1 window controller. In my subclass of NSWindowController I can put my window zoomed like this:
override func windowDidLoad() {
super.windowDidLoad()
window?.zoom(self) // Or window?.setFrame(NSScreen.mainScreen()!.visibleFrame, display: true)
}
But when I want to change my window's content view controller, that size doesn't remain same size and I have to tell to zoom it again and that is ugly animation. How can I manage to keep my window's zoomed even if I change my window's content view controller?
UPDATE:
I manage to get it work. In my IBAction when my another window controller is zoomed, I did this when I want change content view controller.
secondViewController!.view.frame = contentViewController!.view.frame
contentViewController = secondViewController

Resources