Skip to content

Instantly share code, notes, and snippets.

@Shubham0812
Last active June 28, 2020 05:37
Show Gist options
  • Save Shubham0812/54803ed76701db3a3be0e79f08281dab to your computer and use it in GitHub Desktop.
Save Shubham0812/54803ed76701db3a3be0e79f08281dab to your computer and use it in GitHub Desktop.
extension UIButton {
// padding - the spacing between the Image and the Title
func centerTitleVertically(padding: CGFloat = 12.0) {
guard let imageViewSize = self.imageView?.frame.size, let titleLabelSize = self.titleLabel?.frame.size
else {
return
}
let totalHeight = imageViewSize.height + titleLabelSize.height + padding
self.imageEdgeInsets = UIEdgeInsets(
top: -(totalHeight - imageViewSize.height) / 2,
left: 0.0,
bottom: 0.0,
right: -titleLabelSize.width
)
self.titleEdgeInsets = UIEdgeInsets(
top: 0.0,
left: -imageViewSize.width,
bottom: -(totalHeight - titleLabelSize.height),
right: 0.0
)
self.contentEdgeInsets = UIEdgeInsets(
top: 0.0,
left: 0.0,
bottom: titleLabelSize.height,
right: 0.0
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment