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.
Related
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
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 😊
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
I am using UITextfield as a search field to allow users filtering the content. When a user open the UI, the UITextField will show a placeholder message "search" in the center of the textfield. I want to add a leftview icon right before the placeholder text(see below code). The leftwiew icon will be shown on the very left of the text field. I want to place it right before the text. How can I find the right position for it?
searchText.placeholder = "Search"
searchText.leftView = UIImageView(image: UIImage(named: "cion_03.png"))
searchText.leftView?.contentMode = .ScaleAspectFit
searchText.leftViewMode = .Always
You forgot to set frame for view. Try to set proper x position to your'e image.
let icon = UIImageView(image: UIImage(named: "cion_03"))
icon.frame = CGRectMake(0.0, 0.0, icon.image.size.width+10.0, icon.image.size.height);
icon.contentMode = UIViewContentMode.Center
self.textField.leftView = icon;
self.textField.leftViewMode = UITextFieldViewMode.Always
Your best bet, for this level of customization, is to subclass UITextField. You will be able to modify the frame of the placeholder text, to modify its position.
You should override placeholderRectForBounds:. Once that is changed, you might find that you will need to override textRectForBounds: to match the placeholder text position.
I've seen this approach used successfully, to accommodate certain fonts.
I spent a day trying to do this using the UITextField subclass. Got it mostly working but I also needed to change the text alignment from centered to left-aligned when editing. I ended up solving this by constraining an image view to the front of the text field and it worked great. I was even able to animate them back and forth and everything.
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 magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: #image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];
Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.
I have also tried setting the contentMode to UIViewContentModeScaleAspectFill but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.
Have you tried setImage instead of setBackgroundImage?
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;
In Swift...
button.imageView?.contentMode = . scaleAspectFit
Storyboard
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:
Swift 4.2
myButton.imageView!.contentMode = .scaleAspectFit
Swift earlier version
myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
Swift 4.X and 5.X
button.imageView!.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
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.