How to set an image to the UIButton in Swift - ios

I want to set an image to the button
But every time when I add an image to the button it shows a huge image and the image doesn't fit the button size. Like this:

yourButton.clipsToBounds = true
yourButton.contentMode = .scaleAspectFill
// Use setBackgroundImage or setImage
yourButton.setBackgroundImage(UIImage(named: "yourImage"), for: .normal)

Change button style from 'Plain' to 'Default'
Click here for the image reference

Related

Center title on the button image

I want to change button image and title programmatically. When design button image and title (plain) on storyboard it is centered no problem. But when ı use;
startButton.setImage(UIImage(named: "StartButton"), for: .normal)
startButton.setAttributedTitle(NSAttributedString(string: "START"), for: .normal)
to change button image and title programmatically. Button title not centered on image. It looks like it's on the left of the image.
This may helps you.
use setBackgroundImage instead of setImage
startButton.setBackgroundImage(UIImage(named: "StartButton"), for: .normal)
hope this solution will solve your problem 😊

Insert Icon in Navigation Bar / make BarButtonItem not clickable Swift iOS

I would like to place a bluetooth icon in my Navigation Bar, to display a connected / disconnected status.
I tried to add a BarButtonItem, set the image as my bluetooth icon, and disabled and enabled this Button. This works fine so far, and is looking ok to me, but I don't want to have this button clickable, so that it doesn't change the color on clicking on the icon.
Is this possible, or is there a way to put a UIImageView into the Navigation Bar?
Thanks!
Try disabling touch events using:
myBarButtonItem.isUserInteractionEnabled = false
navigationItem.rightBarButtonItem?.isEnabled = false
Add following code will fix your issue.
let btnBluetooth = UIButton()
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .normal)
btnBluetooth.setImage(#imageLiteral(resourceName: "icon_bluetooth"), for: .highlighted)
btnBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: btnBluetooth)
self.navigationItem.rightBarButtonItem = barButton
This code will add custom button where you can manage image for normal and highlight mode. For bluetooth state management, change tintColor of UIButton which you have added in UIBarButtonItem as custom view.
If you click on button, this will not change color of image.
If you don't want to add UIButton you can add UIImageView by following code.
let imgBluetooth = UIImageView(image: #imageLiteral(resourceName: "icon_bluetooth"))
imgBluetooth.tintColor = .red
let barButton = UIBarButtonItem(customView: imgBluetooth)
self.navigationItem.rightBarButtonItem = barButton
Also, make sure you have selected Render As as Template Image for your bluetooth icon which is added inside Assets.xcassets to affect tintColor. Otherwise image will be display as original. See follow:

UISlider with different minimumTrackTintColor like shown in image preview

I need to implement change UISlider minimumTrackColor with different color according to user selection. Same like shown in image please check.Check out this example UISlider
you have to setMinimumTrackImage
slider.setMinimumTrackImage(UIImage(named: "CLR"), forState: .Normal)
CLR.png is

make image has no touchable frame in swift

I want to add an image over a button. When I click on it, I will click on the button not the image. I tried to set an image for the button (beside its text), but I failed to resize it using this code. Please help me either write the correct code or to make an image over the button. It shouldn't block the button, which would annoy the user when the user wants to click the button below the image.
var img : UIImageView!
img = UIImageView(frame:CGRectMake(0, 100, 0.10, 0.100));
img.image = UIImage(named:"plus.png")
createGroupButton.setImage(img.image , forState: UIControlState.Normal)
Bharat's answer should work for you. However, in case you have an image which you want to place on top of any element and you want it to transfer touches to the elements behind it you need to set:
image.userInteractionEnabled = false
this also works for UIViews and for other subclasses of UIView.
You can set UIButton's background image like this.
button.setBackgroundImage(UIImage(named:"plus.png"), forState: .Normal)
Hope this helps.

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

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 doesn't respect that setting.
Am I looking in the wrong place or is it not possible?
While this may not be quite what you're after - the background image does scale.
Try this:
[button setImage:image forState:UIControlStateNormal];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
In Swift:
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
Remember to set Control Alignment settings for the button
Inside Attribute Inspector first set Type "Custom" and Style "Default" then Set alignment Horizontal and Vertical both "fill".
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
Just do (From Design OR From Code):
From Design
Open your xib OR Storyboard.
Select button
Inside Attribute Inspector (Right side) > In "Control" section > select last 4th option for both Horizontal and Verical.
[For Point#3: Change Horizontal and vertical Align to UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]
From Code
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
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
Updating this post will full answer for Swift 5:
//Set button's image
let image = UIImage(systemName: "heart.fill") //Your image here
button.setImage(image, for: .normal)
//Optional: Remove background image if you added one on accident
button.setBackgroundImage(nil, for: .normal)
//Optional: Set button's title (displayed next to button's image)
button.setTitle(nil, for: .normal)
//The button's "content" includes the title label and image
//So we set the button's content to fill the button
//If title label is nil, the content will only show the image
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
//Set button's image to desired content mode (aspectFit avoids distortion)
//This restricts the image from resizing to fill the entire content area
button.imageView?.contentMode = .scaleAspectFit
Try setting the Clip subviews property in Interface Builder to true.
Hope that helps.
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.
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.

Resources