How to adjust an UIButton's imageSize?

前端 未结 16 2106
时光说笑
时光说笑 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: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))

提交回复
热议问题