Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton with text on left and image on right swift [duplicate]

Tags:

ios

swift

swift3

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: enter image description here

like image 701
Nevin Jethmalani Avatar asked Jul 28 '17 17:07

Nevin Jethmalani


3 Answers

Use

button.semanticContentAttribute = .forceRightToLeft

For other methods you can refer this and this.

like image 189
Mohammad Sadiq Avatar answered Nov 03 '22 18:11

Mohammad Sadiq


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.

enter image description here

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

like image 45
Krunal Avatar answered Nov 03 '22 18:11

Krunal


You can update the image and title insets either programmtically or inside attribute inspector. Here is the screenshot:

enter image description here

like image 24
Hussain Shabbir Avatar answered Nov 03 '22 18:11

Hussain Shabbir