Why does a custom UIButton image does not resize in Interface Builder?

前端 未结 10 1171
你的背包
你的背包 2020-12-24 00:45

Why does a custom UIButton image not resize with the button?

I set its view mode to Scale to Fill in Interface Builder, but unlike a UIImageView image, it d

相关标签:
10条回答
  • 2020-12-24 01:23

    While this may not be quite what you're after - the background image does scale.

    0 讨论(0)
  • 2020-12-24 01:25

    Just get a real UIimageview and place a UIbutton on top of it and via layout menu set it to back. Then you can scale your image properly.

    0 讨论(0)
  • 2020-12-24 01:26

    I know this is old, but I think that the correct answer is that you should set the content mode to the "hidden" UIImageView that is inside the UIButton:

    button.imageView.contentMode = UIViewContentModeScaleAspectFill;
    

    This will change the content mode for the images views of the images set with:

    [button setImage:yourAwesomeImage forState:UIControlStateNormal]; 
    //Or any other state
    
    0 讨论(0)
  • 2020-12-24 01:29

    Try setting the Clip subviews property in Interface Builder to true.

    Hope that helps.

    0 讨论(0)
  • 2020-12-24 01:30

    The best solution I found so far in Swift 2.1 is the following:

    self.imageButton.imageView?.clipsToBounds = true
    self.imageButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    

    This will scale the image so it has an aspect fit in the imageView.

    0 讨论(0)
  • 2020-12-24 01:33

    Interface Builder

    To get images in UIButtons to scale the same way as UIViews in interface Builder follow these steps:

    Select your UIButton and from the Identity Inspector add the following to User Defined Runtime Attributes:

    imageView.contentMode Number 1

    Where number is the enum number of contentMode, eg:

    0 = Scale to fill 
    1 = Aspect Fit 
    2 = Aspect Fill
    etc..
    

    Then open the Attributes editor for your UIButton and under Control set the Alignment to Fill (last button on right) for both horizontal and vertical.

    SWIFT 3

    Note you can also do this in code with the following:

    button.imageView?.contentMode = .scaleAspectFit
    button.contentVerticalAlignment = .fill
    button.contentHorizontalAlignment = .fill
    
    0 讨论(0)
提交回复
热议问题