0
votes

I have a problem with positioning NSViews on screen from code inside my app.

Orientation system on Mac is like in math, so 0,0 is in bottom left corner.

So, if you want to position your View on top left corner, it frame origin should be 0,height_of_window/parentView. Here starts my problems.

My window have size NSRect (x=0, y=0), (width=643, height=469)
I want to position my custom view in top left corner so I use:

NSRect rect = NSMakeRect(0, superviewRect.size.height, 329, 214);

to reach top of my screen but nothing shows up. The view is drawn outside windows' bounds. But when I aim with some smaller number like this:

NSRect rect = NSMakeRect(0, 200, 329, 214);

My View shows up like this: Image

And it is placed somewhere, not at the top but at least i can see it. How I can measure this top left corner from code to place it in the right place, when window got resized or similar case? I have Retina display, this is causing my bad calculations?

I hope you can understand what I menat!

1

1 Answers

1
votes

As you said the coordinate system has (0,0) in bottom left corner.

This means that bounds of the inner view are relative to the bottom left corner of the view itself. So you should subtract the height of the internal component from the total height to find the correct position. So:

NSRect rect = NSMakeRect(0, superviewRect.size.height - 214, 329, 214);

You should really consider reading the View programming guide, especially here and here.

Mind that you can flip the coordinate system by overriding isFlipped for the parent view, so that everything is interpreted with (0,0) in top left corner.