将按钮图像对齐到UIButton的右边缘

时间:2015-09-03 01:14:13

标签: ios xcode uibutton uiimage uiedgeinsets

根据标题文字有很多关于对齐按钮图像的线索,但我找不到任何关于将图像对齐到按钮右侧的信息。

这没有效果:

button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);

如何将图像右键对齐按钮?可以在故事板中完成吗?

14 个答案:

答案 0 :(得分:46)

语义:视图从右到左强制为我enter image description here

工作

答案 1 :(得分:17)

<强>故事板

属性标签&gt;确保为“图片”选择了“内容边缘”标签:

1

然后你改变'Left'属性不对,这就是你以编程方式做的事情。换句话说,你从左边填充

UIEdgeInsetsMake = TOP |左| BOTTOM | RIGHT

以编程方式:

button.imageEdgeInsets = UIEdgeInsetsMake(0, 100, 0, 0);

注意:您可能还需要更改标题插入,具体取决于图像的参考位置

答案 2 :(得分:14)

我找到了一个棘手的方法 更新按钮的firebase.auth().createUserWithEmailAndPassword(email, password).then(function(){ var user = firebase.auth().currentUser; user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() { // Update successful. }, function(error) { // An error happened. }); 约束

试试这个

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

答案 3 :(得分:12)

有几种不同的选择。

使用semanticContentAttribute

button.semanticContentAttribute = .forceRightToLeft

您也可以从界面(storyboard)布局设置语义属性。

enter image description here


或  使用包含UIButton和UIImage的UIView ,如此快照中所示。

  • 在UIView中设置图像右侧并禁用用户交互,因此用户操作/点按将传递给按钮。
  • 在UIView中添加尺寸与UIView相同的按钮,透明背景色并覆盖图像。
  • 设置按钮的内容偏移量,如此快照中所示。

enter image description here

这样可以轻松处理按钮操作,属性和自定义图像位置&amp;尺寸根据您的要求。试试这个。

答案 4 :(得分:11)

在Swift中以干净的方式执行此操作

button.semanticContentAttribute = .forceRightToLeft

希望这有帮助

答案 5 :(得分:5)

将其设为默认语义,即未指定或强制从左到右

并在button.imageEdgeInsets中将其设置为

UIEdgeInsets(顶部:0,左:self.view.frame.size.width - (图像大小+对齐空间),底部:0,右:0)

这将确保无论视图大小如何,它都将始终对齐按钮右侧的图像

答案 6 :(得分:3)

简单方法

使用扩展程序在右侧使用自定义偏移设置图像

   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
    }
}

答案 7 :(得分:1)

就像这样:

btn.imageView?.translatesAutoresizingMaskIntoConstraints = false
btn.imageView?.centerYAnchor.constraint(equalTo: btn.centerYAnchor, constant: 0.0).isActive = true
btn.imageView?.trailingAnchor.constraint(equalTo: btn.trailingAnchor, constant: 0.0).isActive = true
btn.imageView?.contentMode = .right

答案 8 :(得分:1)

对我有用:

<app-two></app-two>

答案 9 :(得分:0)

尝试以下代码:

btn.contentHorizontalAlignment = .right

答案 10 :(得分:0)

要使图像向右对齐,“语义”选项似乎是最好的“强制从右向左”。 此外,我想再添加一件事。您可以通过此选项保持按钮图像的形状。 User Defined Runtime Attributes

答案 11 :(得分:0)

这对我有效,方法是设置:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

然后对于内部图像,将右插图设置为0。

button.imageEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0);

答案 12 :(得分:0)

这对我有用

btn.imageView?.translatesAutoresizingMaskIntoConstraints = false
btn.imageView?.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0.0).isActive = true
btn.imageView?.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true

答案 13 :(得分:0)

SWIFT 5

button.imageEdgeInsets = UIEdgeInsets(top: 0, left: (bounds.width - 16), bottom: 0, right: 0)
相关问题