我要创建带有this之类图像的按钮
这是我的代码,但其他型号的iPhone却不是这样:
extension ButtonWithImage {
func alignVertical(spacing: CGFloat = 6.0) {
guard let imageSize = self.imageView?.image?.size else { return }
guard let titleSize = titleLabel?.intrinsicContentSize else{return}
// Title Edge Insets
let titleEdgeInsets = UIEdgeInsets(top: 0.0 ,
left: -imageSize.width,
bottom: -(self.frame.size.height + 2*spacing),
right: 0.0)
self.titleEdgeInsets = titleEdgeInsets
self.imageEdgeInsets = UIEdgeInsets(top: 0.0,
left: 0.0,
bottom: 0.0,
right: -titleSize.width)
self.contentEdgeInsets = UIEdgeInsets(top: spacing,//edgeOffset,
left: 0.0,
bottom: spacing,//edgeOffset,
right: 0.0)
}
}
titleEdgeInsets中的bottom值在什么型号的iPhone上都可以使用?
答案 0 :(得分:0)
请使用以下扩展名以方便使用
extension UIButton {
func alignCenter(withPadding: CGFloat = 6.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),
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
)
}
}