Skip to content

Instantly share code, notes, and snippets.

@ahikmatf
Created December 26, 2019 02:23
Show Gist options
  • Save ahikmatf/cc6255bb0af0e9cade247ba483c807a4 to your computer and use it in GitHub Desktop.
Save ahikmatf/cc6255bb0af0e9cade247ba483c807a4 to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/a/35277239
extension UIButton {
func alignImageAndTitleVertically(padding: CGFloat = 5.0) {
let imageSize = self.imageView!.frame.size
let titleSize = self.titleLabel!.frame.size
let totalHeight = imageSize.height + titleSize.height + padding
self.imageEdgeInsets = UIEdgeInsets(
top: -(totalHeight - imageSize.height),
left: 0,
bottom: 0,
right: -titleSize.width
)
self.titleEdgeInsets = UIEdgeInsets(
top: 0,
left: -imageSize.width,
bottom: -(totalHeight - titleSize.height),
right: 0
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment