UIbutton的图片和文字位置

默认居中显示的情况

let space = 8.0
let image = UIImage(named: model.icon)
let size = image?.size
//图片在上,文字在下
 item.imageEdgeInsets = UIEdgeInsets(top: -CGFloat(item.titleLabel?.intrinsicContentSize.height ?? 18) - space, left: 0, bottom: 0, right: -CGFloat(item.titleLabel?.intrinsicContentSize.width ?? 56))
 item.titleEdgeInsets = UIEdgeInsets(top: 0, left: -CGFloat(size?.width ?? 56), bottom: -CGFloat(size?.height ?? 56) - space, right: 0)

//图片在下,文字在上
 item.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -CGFloat(item.titleLabel?.intrinsicContentSize.height ?? 18) - space, right: -CGFloat(item.titleLabel?.intrinsicContentSize.width ?? 56))
 item.titleEdgeInsets = UIEdgeInsets(top: -CGFloat(size?.height ?? 56) - space, left: -CGFloat(size?.width ?? 56), bottom: 0, right: 0)

// 图片在左,文字在右
 item.imageEdgeInsets = UIEdgeInsets(top: 0, left: -space / 2, bottom: 0, right: space / 2)
 item.titleEdgeInsets = UIEdgeInsets(top: 0, left: space / 2, bottom: 0, right: -space / 2)

// 图片在右,文字在左
 item.imageEdgeInsets = UIEdgeInsets(top: 0, left: CGFloat(item.titleLabel?.intrinsicContentSize.width ?? 56) + space / 2, bottom: 0, right: -CGFloat(item.titleLabel?.intrinsicContentSize.width ?? 56) - space / 2)
 item.titleEdgeInsets = UIEdgeInsets(top: 0, left: -CGFloat(size?.height ?? 56) - space / 2, bottom: 0, right: CGFloat(size?.height ?? 56) + space / 2)
image.png

你可能感兴趣的:(UIbutton的图片和文字位置)