How to resize UIViews to fit within a smaller parent view

Go To StackoverFlow.com

0

I have several view parameters saved in Core Data scaled to fit within self.view. When I fetch the view parameters I want to rescale the views to fit within a smaller parent view (newView). How do I get the viewSelected subviews to resize within the newView parent view?

newView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
newView.autoresizesSubviews = YES;
for (Shape *shape in shapes) {
    ShapeView *viewSelected = [[SquareView alloc]init];

    [viewSelected setAutoresizingMask:UIViewAutoresizingFlexibleHeight |
             UIViewAutoresizingFlexibleWidth];
    viewSelected.frame = CGRectMake(shape.x,shape.y,shape.shapeSize,shape.shapeSize);
    [newView addSubview:viewSelected];
}
[self.view addSubview:newView];
2012-04-04 16:34
by John Green


0

There are probably multiple ways of handling this, but I can think of two off of the top of my head.

  1. Manually - What the autoresizing mask gives you is deterministic, so calculate it out.

  2. Create an intermediate UIView that is the size of self.view, add your fetched views to it, and resize the intermediate view to the size of the "new smaller parent". Then your subviews will have resized appropriately. Then remove them from the intermediate view and add them to the desired destination view.

2012-04-04 18:12
by Joseph DeCarlo
Can you provide a working example for this please ? Joseph DeCarl - Sakkeer Hussain 2015-08-04 14:15
Ads