I'm trying to make a UIButton that has a UIImage on either side (left and right) of the button's title. Ideally, the images would be pinned to the left and right sides of the button, and the title would be centered, but I can live with the images being right next to the title label I suppose. I have been able to add one image, which appears right before the title, but how would I add a second?
You will also learn how to do the basic customization of UIButton like setting button tint color, background color and making your button call a function when tapped. ButtonType. close. To create a regular, system type button in Swift you will need to create a new instance of UIButton class.
In Short: playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal) For Swift 3: let playButton = UIButton(type: .custom) playButton.setImage(UIImage(named: "play.png"), for: .normal)
To create a new UIButton, set its width, height, and position the button within a view programmatically, you can do with CGRect class. For example: Setting text on a button is very simple. It can be done with a function call setTitle (_ title: String?, for state: UIControl.State) . A title on the button can be set for different button states.
ButtonType. close. To create a regular, system type button in Swift you will need to create a new instance of UIButton class. To create a button of a different type like for example ButtonType. close, you will do: To create a new UIButton, set its width, height, and position the button within a view programmatically, you can do with CGRect class.
I know this is an old question but since no code was provided and someone asked for it I figured I would share my solution for this.
What I did was create a new class which subclasses a UIButton. I made sure that you can see all the changes you do instantly in your interface builder as well. So you can just drag in a UIButton. Specify the class to your own class and it will allow you to set the images and see it being drawn instantly
Here is what I did to achieve this
import UIKit
@IBDesignable
class AddProfilePictureView: UIButton {
@IBInspectable var leftHandImage: UIImage? {
didSet {
leftHandImage = leftHandImage?.withRenderingMode(.alwaysOriginal)
setupImages()
}
}
@IBInspectable var rightHandImage: UIImage? {
didSet {
rightHandImage = rightHandImage?.withRenderingMode(.alwaysTemplate)
setupImages()
}
}
func setupImages() {
if let leftImage = leftHandImage {
self.setImage(leftImage, for: .normal)
self.imageView?.contentMode = .scaleAspectFill
self.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: self.frame.width - (self.imageView?.frame.width)!)
}
if let rightImage = rightHandImage {
let rightImageView = UIImageView(image: rightImage)
rightImageView.tintColor = COLOR_BLUE
let height = self.frame.height * 0.2
let width = height
let xPos = self.frame.width - width
let yPos = (self.frame.height - height) / 2
rightImageView.frame = CGRect(x: xPos, y: yPos, width: width, height: height)
self.addSubview(rightImageView)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With