iOS: Autosizing an UIImage programmatically

2019-09-07 12:32发布

I have an UIImage in my app, and I had to uncheck the auto layout box and set autosizing with the options shown in the image below so the image will show properly in both screen sizes (3.5 inch, and 4 inch)

enter image description here

My question is, how can I set these options in code?

I tried this but it doesn't seem to work? Am I doing something wrong?

file.m

// Autosizing ImageOne
ImageOne.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleTopMargin;

3条回答
Explosion°爆炸
2楼-- · 2019-09-07 12:37

The presence of the I-beam on the left, right, and bottom means that they're fixed (i.e. not flexible), so you should not use UIViewAutoresizingFlexibleLeftMargin, UIViewAutoresizingFlexibleRightMargin, or UIViewAutoresizingFlexibleBottomMargin. The absence of the top I-beam means that you should employ UIViewAutoresizingFlexibleTopMargin.

The presence of the width arrow means that the width is flexible, and that you should therefore include UIViewAutoresizingFlexibleWidth. But the absence of the height arrow means that UIViewAutoresizingFlexibleHeight should not be used.

Thus, that yields:

self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

By the way, you can verify this empirically. Temporarily add a view in Interface Builder using the combinations of springs and struts that you shared with us:

mask

Then you can use a routine like the following to examine the auto resizing mask of that view:

- (void)examineAutosizingMask:(UIView *)view
{
    NSDictionary *masks = @{@"UIViewAutoresizingFlexibleBottomMargin" : @(UIViewAutoresizingFlexibleBottomMargin),
                            @"UIViewAutoresizingFlexibleHeight"       : @(UIViewAutoresizingFlexibleHeight),
                            @"UIViewAutoresizingFlexibleLeftMargin"   : @(UIViewAutoresizingFlexibleLeftMargin),
                            @"UIViewAutoresizingFlexibleRightMargin"  : @(UIViewAutoresizingFlexibleRightMargin),
                            @"UIViewAutoresizingFlexibleTopMargin"    : @(UIViewAutoresizingFlexibleTopMargin),
                            @"UIViewAutoresizingFlexibleWidth"        : @(UIViewAutoresizingFlexibleWidth) };

    for (NSString *key in masks) {
        if (view.autoresizingMask & [masks[key] longValue]) {
            NSLog(@"%@", key);
        }
    }
}

The above routine will confirm you that you want UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth.

You can also confirm this in the debugger, as it provides a good tool to examine the configuration of views (their frames and autosizing masks). Run the app in the debugger, and once the view has been presented, hit the pause button. Then, at the (lldb) prompt, type:

(lldb) po [[UIWindow keyWindow] recursiveDescription]

That will generate output like so:

<UIWindow: 0x8c61080; frame = (0 0; 320 480); gestureRecognizers = <NSArray: 0x8c60070>; layer = <UIWindowLayer: 0x8c609b0>>
   | <UIView: 0x8c614d0; frame = (0 0; 320 480); autoresize = RM+BM; layer = <CALayer: 0x8c615b0>>
   |    | <UIImageView: 0x8d212a0; frame = (80 159; 160 263); autoresize = W+TM; userInteractionEnabled = NO; layer = <CALayer: 0x8d213a0>>

As you can see, this is also reporting that the width and top margin autosizing masks have been used (where it says W+TM). This output can also be useful for diagnosing the frame size.


By the way, in addition to setting the auto sizing bit mask as discussed, you may want to confirm the content mode and the clipping setting. Notably, you may also want to make sure you clip the image view to its bounds (or else the image may overflow the bounds, misleading you on the actual size of the frame):

self.imageView.clipsToBounds = YES;
查看更多
迷人小祖宗
3楼-- · 2019-09-07 12:38

If you want to take the image full screen then make your setting like this:

enter image description here

Hope this helps.. :)

查看更多
可以哭但决不认输i
4楼-- · 2019-09-07 12:45

Set the UIImageView contentMode property to UIViewContentModeScaleAspectFit

ImageOne.contentMode = UIViewContentModeScaleAspectFill;
ImageOne.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleTopMargin;
查看更多
登录 后发表回答