0
votes

I have NSView (Parent) that can have any number of children NSView's. User adds new views using buttons like by adding image/background view etc. In general parent nsview can have any size let's suppose 1200x1800 and child views can also have any size for example 600x900 each (let's suppose i have 4 child views with below location. These are covering whole parent view. Also Parent view is not directly content view of main window.

child view 1 (0,0,600,900)
child view 2 (601,0,600,900)
child view 3 (0,901,600,900)
child view 4 (601,901,600,900)

Feature of the app is that it can save all locations/attributes of parent as well as child view in xml and that later can be imported.

Problem: Now when working on small screens like MacBook 13", a 1200x1800 size overflows and either needs to have scrolling or scale to fit feature. I don't want scroll feature but need a way that when I load parent view of 1200x1800 size it automatically fits into available screen by maintain it's aspect ratio.

Since subviews info is also saved and can be loaded from xml so i need same feature for them. I add all subviews using "addSubview" feature.

One solution i thought is to relatively change (using width and heigh ratio) the location coordinates of all view with respect to current width/height of window and it's sort of worked but some time converting coordinates back and forth loose by 1 to 2 pixels (math round and ceil issues).

I have also tried NSAffineTransform in parent view's drawRect but do not worked. Below is the code, even if it works for parent what about child views?

- (void) drawRect: (NSRect) dirtyRect {
//Just for testing using hardcoded values
    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSAffineTransform *transform = [[NSAffineTransform alloc] init];
    [transform translateXBy:100 yBy:100];
    [transform scaleBy:-2.0];
    [transform set];
    [[NSGraphicsContext currentContext] restoreGraphicsState];
}

Auto resizing also does not worked here.

So I just want all views to fit in current available space with maintaining their locations. I am struggling with this issue from past one week but no solution. Is there anything i am missing or doing wrong. I think there must be some good way to handle it but not able to figure it out.

Please help.

1
Can any one please help on this?M P

1 Answers

0
votes

After much research i found solution using [myview scaleUnitSquareToSize:mysize];

It works well.