Align button image to right edge of UIButton Align button image to right edge of UIButton xcode xcode

Align button image to right edge of UIButton


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


I found a tricky wayUpdate the constrains for the UIImageView of the Button

try this

button.imageView?.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -8.0).isActive = truebutton.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 = falsebutton.imageView?.translatesAutoresizingMaskIntoConstraints = false


There are several different options to do it.

Use 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.

  • 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.

enter image description here

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