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
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;
Swift 4.2
myButton.imageView!.contentMode = .scaleAspectFit
Swift earlier version
myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
In Swift...
button.imageView?.contentMode = . scaleAspectFit
Have you tried setImage
instead of setBackgroundImage
?
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.
Property image.
You must define specific property in Runtime Attributes of Identity inspector – imageView.contentMode
, where you set value relatively to rawValue
position of enum UIViewContentMode
. 1 means scaleAspectFit.
And button's alignment, in Attributes inspector: