I want to put an image on the right side of the button and the text on the left side. But I want the text to be 20 points from the left margin of the button and the image to be 20 points from the right margin. How would I do that? I want to use the native image and label within a UIButton, I don't want to put anything on top of the button. Here is an example:
Asked
Active
Viewed 1.5k times
11

Nevin Jethmalani
- 2,726
- 4
- 23
- 58
3 Answers
10
Use
button.semanticContentAttribute = .forceRightToLeft

Mohammad Sadiq
- 5,070
- 28
- 29
-
Hey, just a quick comment that actually using the semantic content attribute for this is not a good idea. It messes up with accessibility, especially with VoiceOver. If you are doing swipes to the right to navigate to the next element, and you get to a button that is forced to .forceRightToLeft, another swipe to the right will actually reverse VoiceOver's navigation, and will move the focus back to the previous element. If you swipe right again, you are back on the button that is forced right-to-left, and so on. So the user gets basically stuck in a focus trap, and it can be quite confusing. – dadederk Feb 23 '22 at 12:14
7
Very simple and easy solution: 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.
This will make easy to handle button action, properties and customise image position & size according to your requirement. Try this.

Krunal
- 77,632
- 48
- 245
- 261
1
You can update the image and title insets either programmtically or inside attribute inspector. Here is the screenshot:

LinusGeffarth
- 27,197
- 29
- 120
- 174

Hussain Shabbir
- 14,801
- 5
- 40
- 56