How to not stretch an image in UIButton

前端 未结 7 747
深忆病人
深忆病人 2020-12-23 11:35

I\'m trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnify

相关标签:
7条回答
  • 2020-12-23 11:42

    Make sure the button is a Custom UIButton

    Just using setImage: did not work for me on iOS9.

    But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;

    0 讨论(0)
  • 2020-12-23 11:42

    Swift 4.2

    myButton.imageView!.contentMode = .scaleAspectFit
    

    Swift earlier version

    myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
    
    0 讨论(0)
  • 2020-12-23 11:45

    In Swift...

    button.imageView?.contentMode = . scaleAspectFit
    
    0 讨论(0)
  • 2020-12-23 11:47

    Have you tried setImage instead of setBackgroundImage?

    0 讨论(0)
  • 2020-12-23 11:52

    I think you guys are missing the obvious issue. The image is too large for the button and iOS then has to guess how you want to scale it. Make sure your images are the right size and you won't have this issue. Of course this is for static images and buttons that you know the size for up-front -- not dynamic content.

    0 讨论(0)
  • 2020-12-23 11:58

    Storyboard

    Property image.

    You must define specific property in Runtime Attributes of Identity inspectorimageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.

    And button's alignment, in Attributes inspector:

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