How to adjust an UIButton's imageSize?

前端 未结 16 2098
时光说笑
时光说笑 2021-01-29 21:40

How can I adjust the image size of the UIButton? I am setting the image like this:

[myLikesButton setImage:[UIImage imageNamed:@\"icon-heart.png\"] forState:UICo         


        
相关标签:
16条回答
  • 2021-01-29 22:18

    i think, your image size is also same as button size then you put image in background of the button like :

    [myLikesButton setBackgroundImage:[UIImage imageNamed:@"icon-heart.png"] forState:UIControlStateNormal];
    

    you mast have same size of image and button.i hope you understand my point.

    0 讨论(0)
  • 2021-01-29 22:20

    Swift 3:

    button.setImage(UIImage(named: "checkmark_white"), for: .normal)
    button.contentVerticalAlignment = .fill
    button.contentHorizontalAlignment = .fill
    button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    
    0 讨论(0)
  • 2021-01-29 22:20

    You can also do that from inteface builder like this.

    I think it's helpful.

    0 讨论(0)
  • 2021-01-29 22:22

    With the help of Tim C's answer, I was able to create an extension on UIButton using Swift that allows you to specify the image frame by using the .setImage() function with an extra frame parameter

    extension UIButton{
    
        func setImage(image: UIImage?, inFrame frame: CGRect?, forState state: UIControlState){
            self.setImage(image, forState: state)
    
            if let frame = frame{
                self.imageEdgeInsets = UIEdgeInsets(
                    top: frame.minY - self.frame.minY,
                    left: frame.minX - self.frame.minX,
                    bottom: self.frame.maxY - frame.maxY,
                    right: self.frame.maxX - frame.maxX
                )
            }
        }
    
    }
    

    Using this, if you wanted to set the frame of a UIButton to CGRectMake(0, 0, 64, 64), and set the image of it to myImage with a frame of CGRectMake(8, 8, 48, 48), you could use

    let button: UIButton = UIButton(frame: CGRectMake(0, 0, 64, 64))
    button.setImage(
        myImage,
        inFrame: CGRectMake(8, 8, 48, 48),
        forState: UIControlState.Normal
    )
    
    0 讨论(0)
  • 2021-01-29 22:23

    When changing icon size with UIEdgeInsetsMake(top, left, bottom, right), keep in mind button dimensions and the ability of UIEdgeInsetsMake to work with negative values as if they are positive.

    Example: Two buttons with height 100 and aspect 1:1.

    left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
    right.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
    

    left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
    right.imageEdgeInsets = UIEdgeInsetsMake(45, 0, 45, 0)
    

    left.imageEdgeInsets = UIEdgeInsetsMake(40, 0, 40, 0)
    right.imageEdgeInsets = UIEdgeInsetsMake(60, 0, 60, 0)
    

    Examples 1 and 3 are identical since ABS(100 - (40 + 40)) = ABS(100 - (60 + 60))

    0 讨论(0)
  • 2021-01-29 22:26

    Tim's answer is correct, however I wanted to add another suggestion, because in my case there was a simpler solution altogether.

    I was looking to set the UIButton image insets because I didn't realize that I could set the content mode on the button's UIImageView, which would have prevented the need to use UIEdgeInsets and hard-coded values altogether. Simply access the underlying imageview on the button and set the content mode:

    myButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    

    See UIButton doesn't listen to content mode setting?

    Swift 3

    myButton.imageView?.contentMode = .scaleAspectFit
    
    0 讨论(0)
提交回复
热议问题