I am able to set the image and text for button. But it is aligned horizontally. I want the image and text to be aligned vertically inside the button. i.e., text below the image<
Swift 4.2 compatible code is here , you just need to call this function
public extension UIButton
{
func alignTextUnderImage(spacing: CGFloat = 6.0)
{
if let image = self.imageView?.image
{
let imageSize: CGSize = image.size
self.titleEdgeInsets = UIEdgeInsets(top: spacing, left: -imageSize.width, bottom: -(imageSize.height), right: 0.0)
let labelString = NSString(string: self.titleLabel!.text!)
let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: self.titleLabel!.font])
self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
}
}
}