Skip to content

Instantly share code, notes, and snippets.

@spiridonkopicl
Last active March 7, 2019 07:14
Show Gist options
  • Save spiridonkopicl/5caf8dd1efa9274843d6f5680ba476d1 to your computer and use it in GitHub Desktop.
Save spiridonkopicl/5caf8dd1efa9274843d6f5680ba476d1 to your computer and use it in GitHub Desktop.
UIButton extension - set title to the left and image to the right
extension UIButton {
func addRightImage(image: UIImage, offset: CGFloat) {
self.setImage(image, for: .normal)
self.translatesAutoresizingMaskIntoConstraints = false
self.imageView?.translatesAutoresizingMaskIntoConstraints = false
self.titleLabel?.translatesAutoresizingMaskIntoConstraints = false
self.imageView?.contentMode = .right
self.titleLabel?.adjustsFontSizeToFitWidth = false
self.titleLabel?.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: offset).isActive = true
self.imageView?.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -offset).isActive = true
self.titleLabel?.trailingAnchor.constraint(equalTo: self.imageView!.leadingAnchor, constant: 0).isActive = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment