有很多关于根据标题文本对齐按钮图像的讨论,但我找不到任何关于将图像仅对齐到按钮右侧的内容。
这没有任何效果:
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
如何使按钮中的图像右对齐?可以在Storyboard中完成吗?
有很多关于根据标题文本对齐按钮图像的讨论,但我找不到任何关于将图像仅对齐到按钮右侧的内容。
这没有任何效果:
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
如何使按钮中的图像右对齐?可以在Storyboard中完成吗?
我发现一个巧妙的方法
更新按钮中UIImageView
的限制
试一试吧
button.imageView?.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -8.0).isActive = true
button.imageView?.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: 0.0).isActive = true
但不要忘记添加这个以使约束生效
button.translatesAutoresizingMaskIntoConstraints = false
button.imageView?.translatesAutoresizingMaskIntoConstraints = false
有几种不同的选项可以实现它。
使用semanticContentAttribute
button.semanticContentAttribute = .forceRightToLeft
您可以从界面(Storyboard)布局中设置语义属性。
或者使用包含UIButton和UIImage的UIView,如此快照所示。
这将使按钮操作、属性和根据您的要求自定义图像位置和大小变得更加容易处理。试试这个。
self.button.frame.size.width - widthOfImage
或其他类似的值即可。 - soulshined尝试以下代码:
btn.contentHorizontalAlignment = .right
在 Swift 中实现清晰的方式
button.semanticContentAttribute = .forceRightToLeft
希望这可以帮到您。
简易方法
使用扩展程序将图像设置在右侧,并自定义偏移量。
extension UIButton {
func addRightImage(image: UIImage, offset: CGFloat) {
self.setImage(image, for: .normal)
self.imageView?.translatesAutoresizingMaskIntoConstraints = false
self.imageView?.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0.0).isActive = true
self.imageView?.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -offset).isActive = true
}
}
self.imageView?.contentMode = .right
来防止图片被拉伸。 - rmp使其成为默认的语义,即未指定或强制从左到右
在 button.imageEdgeInsets 中将其设置为
UIEdgeInsets(top: 0, left: self.view.frame.size.width - (图像尺寸 + 对齐空间), bottom: 0, right: 0)
这将确保不管视图大小如何,它始终会将图像对齐到按钮的右侧
它对我有效:
self.acceptButton.setImage(UIImage(named: image), for: UIControl.State.normal)
self.acceptButton.semanticContentAttribute = .forceRightToLeft
self.acceptButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 0)
因此,代码可能如下所示:
if #available(iOS 15.0, *) {
var configuration = UIButton.Configuration.plain()
let image = UIImage(systemName: "applelogo")
configuration.image = image
configuration.imagePadding = 8.0
configuration.imagePlacement = .trailing
button.configuration = configuration
button.setTitle("Apple", for: .normal)
} else {
let buttonTitle = "Apple"
let titleAttributedString = NSMutableAttributedString(string: buttonTitle + " ")
let textAttachment = NSTextAttachment(image: UIImage(systemName: "applelogo")!)
let textAttachmentAttributedString = NSAttributedString(attachment: textAttachment)
titleAttributedString.append(textAttachmentAttributedString)
button.setAttributedTitle(titleAttributedString, for: .normal)
// When using NSTextAttachment, configure an accessibility label manually either
// replacing it for something meaningful or removing the attachment from it
// (whatever makes sense), otherwise, VoiceOver will announce the name of the
// SF symbol or "Atachement.png, File" when using an image
button.accessibilityLabel = buttonTitle
}