This seems like a very basic question, but I couldn't find the answer anywhere. How do I launch my non-resizable OS X application window in the center of the screen?
If you select your top level window, the Size Inspector will show you a screen similar to this
You can then drag the representation of your window to where you want it to be positioned when launched, or set the specific coordinates.
You can choose the center position for the window in Interface Builder by choosing the size inspector. Otherwise you can call [window center] in applicationDidFinishLaunching
Related
I'm writing a program that needs to get the mouse position in the screen, (not just my view). I need to continuously update variables xPos and yPos. I've heard about subclassing nsview and adding mouseDidMove, but it is never called. (Yes, I set my window view to my custom view.) How can I do this?
You need to set the NSWindow which contains the view to window.acceptMouseMovedEvents = yes. Also if you're just looking for mouse position in the screen consider NSEvent.mouseLocation
Button which show the popover lay on the button left corner of window.
How to show popover only in window bounds?
Attention! Mac OS.
This is not possible. NSPopover is a floating window that is connected to a certain other window but otherwise stays completely on its own. You can however try manually to keep it within certain bounds on screen by calculating a popup position that keeps the popover bounds with that area. But this won't work in all cases anyway (e.g. if the user moves the app window so that there's simply not enough room for the popover).
I'm developing a simple screen capture program under Mac OS, I set the main window to be transparent, and use a NSBox instance contained in main window to specify the area to be captured, here are the significant code and main interface of my program(sorry, no reputation to public image):
capturedImage = CGDisplayCreateImage( kCGDirectMainDisplay );//capture the whole screen
NSRect boxRect = [mBox borderRect];//mBox is an instance of NSBox, lies in main window
capturedImage = CGImageCreateWithImageInRect(capturedImage, boxRect);//obtain an image specified by mBox
I cannot get the right image as I want, I know there may be something wrong with the coordinates but I cannot figure it out, can someone help me with this?
another one, there are 3 buttons on the main window, I want them fixed on the right bottom of main window as I drag and resize the main window, but I have no idea about the layout schema of Interface Builder, is there any good solution? better be real useful code with comments. Thanks very much.
You need to read up about coordinate systems. Your NSBox instance is a view, and asking for its borderRect will return a rect in window coordinates.
You need the rectangle to be in the screen coordinate system, because your whole screen image uses that coordinate system. You can use this method to convert the rect:
NSRect screenBoxRect = [[mBox window] convertRectToScreen:[mBox frame]];
When the OS X dock is shown, it "reserves" some screen area and prevents maximized windows from going behind the dock when maximized. How do I make my own Cocoa application do the same?
A window's "maximizable area" is based on the screen's visibleFrame.
[[NSScreen mainScreen] visibleFrame]
The returned rectangle is always based on the current user-interface settings and does not include the area currently occupied by the dock and menu bar.
Because it is based on the current user -interface settings, the returned rectangle can change between calls and should not be cached.
The rectangle defining the portion of the screen in which it is currently safe to draw your application content.
There is no mention of being able to adjust this visibleFrame rectangle, so I do not think you will have any success influencing the "global" Zoom size.
However, if you only want to adjust the Zoomed size of your app's window, return a smaller rectangle in the NSWindowDelegate method windowWillUseStandardFrame:defaultFrame:.
The standard frame for a window should supply the size and location that are “best” for the type of information shown in the window, taking into account the available display or displays. For example, the best width for a window that displays a word-processing document is the width of a page or the width of the display, whichever is smaller. The best height can be determined similarly. On return from this method, the zoom: method modifies the returned standard frame, if necessary, to fit on the current screen.
This will allow you to take into account a "docked sidebar" when Zooming your app's windows. Other apps will be resized on top of the docked sidebar.
Each time I create any type of cocoa project (document-based, with core-data and/or spotlight importer) and open MainMenu.xib, I get a warning sign in bottom right object palette window corner saying that window is out of screen bounds. Why is this so? Any remedy?
I get a warning sign in bottom right object palette window corner saying that window is out of screen bounds. Why is this so?
Because the window falls at least partially outside the screen bounds.
Any remedy?
Put it completely within the screen bounds. Select the window and use the Content Size & Position section of the Size (⌘3) inspector to move it.
Simply dragging the window by its title bar will not help, since that position is only for display in IB; it doesn't affect the window that will appear in your app, which is what the warning is about. You need to use the Size inspector.
I once had a similar problem when I had a second monitor connected to my MBP.
When the second monitor was set to a low resolution (I think it was 1024x768), Interface Builder came up with that particular warning. (Although the window did fit between the main menu and the Dock).