UIButton won't go to Aspect Fit in iPhone

后端 未结 21 1981
小鲜肉
小鲜肉 2020-12-23 02:47

I have a couple UIButtons, and in IB they\'re set to Aspect Fit, but for some reason they\'re always stretching. Is there something else you have to set? I tried all the d

相关标签:
21条回答
  • 2020-12-23 03:09

    You can use imageWithCGImage as shown above (but without the missing parentheses).

    Also... millions of non-4.0 phones won't work with that code at all.

    0 讨论(0)
  • 2020-12-23 03:14
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    0 讨论(0)
  • 2020-12-23 03:17

    This answer is based on @WernerAltewischer's answer.

    To avoid having connect my button to an IBOutlet to execute the code on it, I subclassed UIButton's class:

    // .h
    @interface UIButtonWithImageAspectFit : UIButton
    @end
    
    // .m
    @implementation UIButtonWithImageAspectFit
    
    - (void) awakeFromNib {
        [self.imageView setContentMode:UIViewContentModeScaleAspectFit];
    }
    
    @end
    



    Now create a custom button in your xib, and set its image (not the background image):

    UIButtonWithImageAspectFit: create and set image


    Then, set its class:

    UIButtonWithImageAspectFit: set custom class

    You're done!
    Instead of
    UIButton without image aspect fit
    your button's image aspect fit is now as expected:
    UIButton with image aspect fit

    0 讨论(0)
  • 2020-12-23 03:19

    I had the same issue, but I couldn't get it to work (perhaps it's a bug with the SDK).

    Eventually, as a workaround, I placed a UIImageView behind my button and set the options I wanted on that, then simply placed a blank UIButton on top of it.

    0 讨论(0)
  • 2020-12-23 03:20

    If you put the image in an UIImageView behind the button, you'll loose the built-in functionality of the UIButton class, such as adjustsImageWhenHighlighted and adjustsImageWhenDisabled, and of course the ability to set different images for different states (without the hazzle of doing this yourself).

    If we want to have an image unstreched for all control states, one approuch is to get the image using imageWithCGImage:scale:orientation, as in the following method:

    - (UIImage *) getScaledImage:(UIImage *)img insideButton:(UIButton *)btn {
    
        // Check which dimension (width or height) to pay respect to and
        // calculate the scale factor
        CGFloat imgRatio = img.size.width / img.size.height, 
                btnRatio = btn.frame.size.width / btn.frame.size.height,
                scaleFactor = (imgRatio > btnRatio 
                               ? img.size.width / btn.frame.size.width
                               : img.size.height / btn.frame.size.height;
    
        // Create image using scale factor
        UIImage *scaledImg = [UIImage imageWithCGImage:[img CGImage]
                                                 scale:scaleFactor
                                           orientation:UIImageOrientationUp];
        return scaledImg;
    }
    

    To implement this we would write:

    UIImage *scaledImg = [self getScaledImage:myBtnImg insideButton:myBtn];
    [myBtn setImage:scaledImg forState:UIControlStateNormal];
    

    This should prevent the image from stretching in all control states. It worked for me, but let me know if it doesn't!

    NOTE: Here we are addressing a problem relating to UIButton, but the insideButton: might as well be insideView:, or whatever one would like to fit the image into.

    0 讨论(0)
  • 2020-12-23 03:21

    [button sizeToFit] worked for me.

    0 讨论(0)
提交回复
热议问题