Scale a UIView automatically on orientation change to scale proportionaltely to fit in parent view

10,512

Solution 1

I've been playing around with it. This is incomplete, just handles the portrait subview scaling, but works ok so far.

if (self.view.bounds.size.width < self.view.bounds.size.height) {
    NSLog(@"view is portrait");
    if (_sview.frame.size.width < _sview.frame.size.height) {
        NSLog(@"subview is portrait");
        [UIView animateWithDuration:0.1
                         animations:^{
                             _sview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
                         }];
    } else {
        NSLog(@"subview is landscape");
    }
} else {
    NSLog(@"landscape");
    if (_sview.frame.size.width < _sview.frame.size.height) {
        [UIView animateWithDuration:0.1
                         animations:^{
                             _sview.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.65, 0.65);
                         }];
    } else {
        NSLog(@"subview is landscape");
    }

}

Solution 2

From the documentation:

The content mode specifies how the cached bitmap of the view’s layer is adjusted when the view’s bounds change.

For an image view, this is talking about the image. For a view that draws its content, this is talking about the drawn content. It does not affect the layout of subviews.

You need to look at the autoresizing masks in place on the subviews. Content mode is a red herring here. If you can't achieve the layout you need using autoresizing masks, then you need to implement layoutSubviews and calculate the subview positions and frames manually.

Solution 3

  • Content modes do not affect the subviews of the view.
  • You would use auto-resizing masks to determine how the subviews are laid out.
  • If that is not sufficient, you may want to implement layoutSubviews

Referenced from: https://stackoverflow.com/a/14111480/1374512

Other useful info on layoutSubviews: https://stackoverflow.com/a/5330162/1374512

Solution 4

How about this :

 self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

Or you can try this :

// horizontal 
   childView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |  UIViewAutoresizingFlexibleRightMargin;

// vertical 
   childView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

// both 
   childView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

If you want any further Reference, I think these are two beautiful Questions :

1) Autoresizing masks programmatically

2) UIView and AutoresizingMask ignored

Share:
10,512
Mr Ordinary
Author by

Mr Ordinary

I am learning for pleasure and profit.

Updated on June 15, 2022

Comments

  • Mr Ordinary
    Mr Ordinary almost 2 years

    A UIImage view set to "Aspect Fit" will automatically dynamically scale it's image to fit within the current bounds of the UIUmageView, while maintaining the image's proportions. A UIView set to "Aspect Fit" does not seem to have this same effect on it's sub-views.

    I have tried to set the parent view via code, with the code below, and tried several variations on the auto-resizing mask ( I am not using auto layout). Am I missing something obvious, or do I need to write a little code to compute the right scale for my sub-view based on the current size of the parent view?

    [self.view setContentMode:UIViewContentModeScaleAspectFit];