Programmatically change initial size of NSWindow - cocoa

I wish to create a NSWindow with an initial size that is programmatically determined (as parameter to [MyWindowController init:]). My current approach is to call [window setFrame:frame] in windowDidLoad within the window controller, but the problem is that the window flashes briefly at the size specified in the nib file before it changes to its new size. I want to avoid this flash. Is it possible to set the initial size of an NSWindow programmatically?

Switch off "Visible At Launch" of the window and call [MyWindowController showWindow:] after you set the frame.

Related

Set initial size for NSWindow

I'm trying to set an initial size for my NSWindow. First, I tried to set size using storyboard.
And then I tried the code below, the both don't work.
NSWindow *mainWindow = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
mainWindow.titleVisibility=NSWindowTitleHidden;
NSRect frame = mainWindow.frame;
frame.size=CGSizeMake(1000, 200);
[mainWindow setFrame:frame display:YES];
I tried to check or uncheck the "Restorable" attribute. How can I set an initial size for NSWindow? Why are these not working?
I found a way to set the initial window size in the storyboard editor of Xcode.
Instead of setting the size of the window, you need to set it on the ViewControllers first element like this:
If you use a storyboard with macOS 10.15.5 or newer, the initial size of the NSWindow may be result of the sum of all containing NSView objects in the contentViewController. This may result in a larger window than in earlier macOS versions.
To fix unexpected large window sizes, search for large NSView objects in your storyboard, and make them smaller. Keep resizing and testing until the initial window size is to your liking.

cocoa: NSTextField with custom font, cursor out of wack

I have an NSTextField with a custom font set up in Interface Builder. When it loads, this is how it shows up:
When the user starts typing, the cursor moves correctly
And then when the user deletes the text it resets to the correct original position
Any ideas why this might be happening?
try to enable the CG layer for the textfield and its superview
.wantsLayer = YES;

How to display NSWindow within the main window in Cocoa?

I want to show a new window when an user taps on a button in the main window, but when I called the separate window using makeKeyAndOrderFront, the window popped up far away from the main window.
Here's what I wrote, in a subclass of NSViewController:
myWindow = NSWindow()
myWindow.setFrame(myView.frame, display: true)
myWindow.contentView = myView
myWindow.makeKeyAndOrderFront(self)
However, this opens a new window outside of the main window. I want to display it within the main window, possibly in the center of the main window (I set the .frame value of myView to be positioned on the center).
So how can I call the new window within the main window? In other words, can I specify where the new window shows up?
You'll need to convert the coordinates and or rect to screen space.
NSView and NSWindow provide methods that begin with "convert" take a look at those.
Sometimes you'll need to chain to convert from view space to window then window to screen.
Keep in mind that a window has an NSScreen screen method and that a window could appear on many different size screens and potentially span multiple screens.

NSScroller inside a NSScrollView does not change position when resizing the window (in Swift)

I have created a NSView with Interface Builder (Xcode 6.1). The NSView has constraints for each direction. When changing the window-size with the mouse manually, the NSView is getting a new size and the NSScroller gets a new position at the correct right border of the window, as expected.
When I set the window size manually (before it is made visible) with
myTextView.enclosingScrollView!.window!.setFrame(theRect)
or with
myTextView.window!.setFrame(theRect)
Then the window resizes well (as soon as it becomes visible), the NSTextView resizes also but the NSScroller (which is automatically part of the NSTextview and has not been created or modified seperately) does not move but stands in the middle of the ScrollView at its old position. The Text entered in the NSScrollView uses the full size and floats behind the NSScroller, which seams to stand in top of the text.
In the case, the Window is visible before setting the Window Size, then the scroll-view is truncated at the right side or bottom side.
When I change the window manually, after settzing the size and getting this behaviour, the NSScroller jumps to it's right position and the NSTextView is not longer truncated. So changing the Window Size manually does something more that I do programmatically.
What must I do, that the Scroller moves with the NSView, like changing the Size of the Window with the Mouse?
This is a typically problem when calling UI-functions from a background thread.
It can besolved, when calling from main-thread or forcing to called by the main-thread with this command:
dispatch_async(dispatch_get_main_queue())
{
// Place the UI Functions here
}
Please have a look at: Open File Dialog crashes in Swift

NSView does not resize when window changes to full screen

When the cocoa application starts, the NSWindow automatically change to full screen resolution. However, the custom view in the window does not resize at all. I am using auto layout so I thought it should change automatically.
I tried to resize the frame in this function but it does work still:
-(void)windowDidResize:(NSNotification *)notification{
// windowView - is custom view in NSWindow
[self.windowView setFrame:self.window.frame];
}
Here is the screenshot: (view not in full size mode.)
Here is the view in full size mode. I have no idea why the view does not enlarge with window.
I think you will need to select these views in Interface Builder, and check the 'Size Inspector' as below.

Resources