iOS: Autosizing an UIImage programmatically

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 17:35:55

问题


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)

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;

回答1:


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:

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;



回答2:


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

Hope this helps.. :)




回答3:


Set the UIImageView contentMode property to UIViewContentModeScaleAspectFit

ImageOne.contentMode = UIViewContentModeScaleAspectFill;
ImageOne.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleTopMargin;


来源:https://stackoverflow.com/questions/25519226/ios-autosizing-an-uiimage-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!