Search code examples
iosscalelayoutsubviews

Scaling a view and all its contents


I'm wondering if there's a way to scale a UIView and all it's subviews.

When I say scaling, I'm not just talking about the frames, I wan't to scale everything so the fonts too.

At the moment I'm doing it manually :

-(void)setScale:(float)scale
{
    // Apply scale on all subviews
    myLabel.font = [UIFont myFontWithSize:baseSize*scale];
    [myButton setScale:scale]
    [myCustomView1 setScale:scale]
    [myCustomView2 setScale:scale]
}
-(void)layoutSubviews
{
    // Apply scale on layout
    myIcon.frame = CGRectMake(posX, posY, baseW*scale, baseH*scale);

    [...]
}

But the setScale method must be defined in all my views, that's quite tedious.

And I've got some problems when I wan't to scale some html text displayed with a UIWebView, this content can't be so easily scaled ...

I've tried another way too : when my view is scaled, it doesn't accept userInteraction so I tried taking snapshots, images are much easier to scale. But it was too heavy for my iphone memory, especially when the views I wan't to scale are hundreds of uicollectionviewCells.

Is there any faster way to do that ?


Solution

  • You could apply a scaling transform to the top level view.

    For example this will scale everything in the view by a factor of 1.5 (50% bigger):

    view.transform = CGAffineTransformMakeScale(1.5, 1.5);