How to Translate iOS UIView Point to Mac NSView


Dave
 

Hi,

I’m converting some Manual Layout code from iOS to Mac.

I’m overriding “layout” in my class, it calculates and sets the Frame Rect for all its subviews. This works ok on Mac, except, as expected, everything is flipped in Y. I’m now trying to figure out how to flip the Y Coordinate.

Given that I have calculated the Frame Rect of a subview correctly, I’m then doing this:

myCalculatedFrameRect = the top, left Rectangle

myRect.origin.y = self.frame.size.height - myCalculatedFrameRect.origin.y;

[mySubview setFrame: myRect];

Which doesn’t work!

self in this case is the View I’m laying out, e.g. the superview of the subview,

What magic do I need to make this work? I was wondering if I should “flip” the View with the “isFlipped” method, but I’m not sure what other problems this might cause and would rather just flip the Coordinate in the layout method…..

All the Best
Dave


Glenn L. Austin
 

Mac uses bottom-left for the origin, which is why everything is "flipped in the Y direction".

You *can* do the calculation to move the origin to top-left and scale the transform in the opposite Y direction, but it's probably just as easy to "re-orient" yourself to bottom-left on macOS.

-- 
Glenn L. Austin, Computer Wizard, AustinSoft.com

On Aug 30, 2017, at 4:14 AM, Dave <dave@...> wrote:

Hi,

I’m converting some Manual Layout code from iOS to Mac.

I’m overriding “layout” in my class, it calculates and sets the Frame Rect for all its subviews. This works ok on Mac, except, as expected, everything is flipped in Y. I’m now trying to figure out how to flip the Y Coordinate.

Given that I have calculated the Frame Rect of a subview correctly, I’m then doing this:

myCalculatedFrameRect = the top, left Rectangle

myRect.origin.y = self.frame.size.height - myCalculatedFrameRect.origin.y;

[mySubview setFrame: myRect];

Which doesn’t work!

self in this case is the View I’m laying out, e.g. the superview of the subview,

What magic do I need to make this work? I was wondering if I should “flip” the View with the “isFlipped” method, but I’m not sure what other problems this might cause and would rather just flip the Coordinate in the layout method…..

All the Best
Dave









Dave
 

Hi,

This seems to do the trick:

-(LTWNativeRectType) adjustRectForPlatform:(LTWNativeRectType) theRect
{
LTWNativeRectType myRect;

myRect = theRect;
myRect.origin.y = self.frame.size.height - myRect.origin.y;
myRect.origin.y -= myRect.size.height;

return myRect;
}

Cheers
Dave

On 30 Aug 2017, at 17:30, Glenn L. Austin <glenn@austinsoft.com> wrote:

Mac uses bottom-left for the origin, which is why everything is "flipped in the Y direction".

You *can* do the calculation to move the origin to top-left and scale the transform in the opposite Y direction, but it's probably just as easy to "re-orient" yourself to bottom-left on macOS.

--
Glenn L. Austin, Computer Wizard, AustinSoft.com

On Aug 30, 2017, at 4:14 AM, Dave <dave@looktowindward.com> wrote:

Hi,

I’m converting some Manual Layout code from iOS to Mac.

I’m overriding “layout” in my class, it calculates and sets the Frame Rect for all its subviews. This works ok on Mac, except, as expected, everything is flipped in Y. I’m now trying to figure out how to flip the Y Coordinate.

Given that I have calculated the Frame Rect of a subview correctly, I’m then doing this:

myCalculatedFrameRect = the top, left Rectangle

myRect.origin.y = self.frame.size.height - myCalculatedFrameRect.origin.y;

[mySubview setFrame: myRect];

Which doesn’t work!

self in this case is the View I’m laying out, e.g. the superview of the subview,

What magic do I need to make this work? I was wondering if I should “flip” the View with the “isFlipped” method, but I’m not sure what other problems this might cause and would rather just flip the Coordinate in the layout method…..

All the Best
Dave







Keary Suska
 

Or have your subclass implement -isFlipped to return YES.

On Aug 30, 2017, at 9:30 AM, Glenn L. Austin <glenn@austinsoft.com> wrote:

Mac uses bottom-left for the origin, which is why everything is "flipped in the Y direction".

You *can* do the calculation to move the origin to top-left and scale the transform in the opposite Y direction, but it's probably just as easy to "re-orient" yourself to bottom-left on macOS.

--
Glenn L. Austin, Computer Wizard, AustinSoft.com

On Aug 30, 2017, at 4:14 AM, Dave <dave@looktowindward.com> wrote:

Hi,

I’m converting some Manual Layout code from iOS to Mac.

I’m overriding “layout” in my class, it calculates and sets the Frame Rect for all its subviews. This works ok on Mac, except, as expected, everything is flipped in Y. I’m now trying to figure out how to flip the Y Coordinate.

Given that I have calculated the Frame Rect of a subview correctly, I’m then doing this:

myCalculatedFrameRect = the top, left Rectangle

myRect.origin.y = self.frame.size.height - myCalculatedFrameRect.origin.y;

[mySubview setFrame: myRect];

Which doesn’t work!

self in this case is the View I’m laying out, e.g. the superview of the subview,

What magic do I need to make this work? I was wondering if I should “flip” the View with the “isFlipped” method, but I’m not sure what other problems this might cause and would rather just flip the Coordinate in the layout method…..

All the Best
Dave

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"