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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32365521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:31:08  来源:igfitidea点击:

Align button image to right edge of UIButton

iosxcodeuibuttonuiimageuiedgeinsets

提问by soleil

There are plenty of threads about aligning a button image according to the title text, but I can't find anything about just aligning the image to the right side of the button.

有很多关于根据标题文本对齐按钮图像的主题,但我找不到任何关于将图像与按钮右侧对齐的内容。

This has no effect:

这没有效果:

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

How can one right-align the image for a button? Can it be done in the Storyboard?

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

回答by Gilad Brunfman

Semantic: Force Right-to-Left on View works for me enter image description here

语义:强制从右到左查看对我有用 在此处输入图片说明

回答by Simon K. Gerges

I found a tricky way Update the constrains for the UIImageViewof the Button

我发现了一个棘手的方法更新UIImageView按钮的约束

try this

尝试这个

button.imageView?.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -8.0).isActive = true
button.imageView?.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: 0.0).isActive = true

but don't forget to add this to make the constrains effective

但不要忘记添加这个以使约束有效

button.translatesAutoresizingMaskIntoConstraints = false
button.imageView?.translatesAutoresizingMaskIntoConstraints = false

回答by soulshined

Storyboard:

故事板

Attributes Tab > Ensure your Content Edge tab is selected for 'image':

属性选项卡 > 确保为“图像”选择了“内容边缘”选项卡:

1

1

Then you alter the 'Left' property not right, which is what your doing programatically. So in other words, your padding it n from the left

然后您将“左”属性更改为不正确,这就是您以编程方式执行的操作。所以换句话说,你从左边填充 n

UIEdgeInsetsMake = TOP | LEFT | BOTTOM | RIGHT

UIEdgeInsetsMake = TOP | 左 | 底部 | 对

Programmatically :

以编程方式:

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

Note: you might have to also alter your titles inset, depending on where it is in reference to your image

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

回答by Krunal

There are several different options to do it.

有几种不同的选择可以做到这一点。

Use semanticContentAttribute

semanticContentAttribute

button.semanticContentAttribute = .forceRightToLeft

You can set semantic attribute from interface (storyboard) layout also.

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

enter image description here

在此处输入图片说明


or Use UIView containing UIButton and UIImage, as shown in this snapshot.


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

  • Set Image right side in UIView and disable user interaction, so user action/tap will be passed to button.
  • Add button with size same as UIView, in UIView, with transparent background color and covering Image.
  • Set content offset for button as shown in this snapshot.
  • 在 UIView 中设置 Image 右侧并禁用用户交互,因此用户操作/点击将传递给按钮。
  • 在UIView中添加大小与UIView相同的按钮,背景色透明,覆盖Image。
  • 设置按钮的内容偏移量,如此快照所示。

enter image description here

在此处输入图片说明

This will make easy to handle button action, properties and customise image position & size according to your requirement. Try this.

这将使处理按钮操作、属性和根据您的要求自定义图像位置和大小变得容易。尝试这个。

回答by Md. Ibrahim Hassan

Clean way to do this in Swift

在 Swift 中执行此操作的简洁方法

button.semanticContentAttribute = .forceRightToLeft

Hope this helps

希望这可以帮助

回答by Dhaval H. Nena

Try below code:

试试下面的代码:

btn.contentHorizontalAlignment = .right

回答by Tharzeez

make it as default semantic i.e unspecified or force left to right

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

and in button.imageEdgeInsets set it as

并在 button.imageEdgeInsets 中将其设置为

UIEdgeInsets(top: 0, left: self.view.frame.size.width - (the image size + the alignment space ) , bottom: 0, right: 0)

UIEdgeInsets(top: 0, left: self.view.frame.size.width - (image size + the alignment space), bottom: 0, right: 0)

this will make ensure that no matter what the view size is ,it will always align the image from right side of the button

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

回答by Musa almatri

Simple way

简单的方法

Using extension to set image on the right side with custom offset

使用扩展在右侧设置带有自定义偏移量的图像

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

回答by Eduardo Parada

It's working for me:

它对我有用:

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)

回答by coletrain

This worked for me by setting:

这通过设置对我有用:

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;

Then for the inner image I set the right inset to 0.

然后对于内部图像,我将正确的插图设置为 0。

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