5

I have the following UIButton I want to add an Image inside to the far right. Like an accessoryView

I am already using backgroundImage and I want to avoid combining the 2 images into 1.

Is it possible to add another image inside the UIButton that is an AccesoryView lets say an image that has a PLUS to the far right? ( I am looking to a way to insert the new image inside the Button)

enter image description here

@luisCien comment

I tried

    [_addImage setImage:[UIImage imageNamed:@"shareMe"] forState:UIControlStateNormal];
   _addImage.contentHorizontalAlignment =
        UIControlContentHorizontalAlignmentRight;

and it looks like this, I would like to image to be on the right side of the text. enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • Here is a very simple solution that can be simply done by IB: [http://stackoverflow.com/a/11847383/5806009](http://stackoverflow.com/a/11847383/5806009) – Soufiane ROCHDI Feb 12 '16 at 15:10

2 Answers2

13

Just add it as a subview and choose your coordinates..Try something like this:

UIImageView *yourPlusSign = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yourPlusSignImageTitle"]];
yourPlusSign.frame = CGRectMake(x, y, width, height);//choose values that fit properly inside the frame of your baseButton
//or grab the width and height of yourBaseButton and change accordingly
yourPlusSign.contentMode=UIViewContentModeScaleAspectFill;//or whichever mode works best for you
[yourBaseButton addSubview:yourPlusSign];
Van Du Tran
  • 6,736
  • 11
  • 45
  • 55
Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
2

You can use the contentHorizontalAlignment property of UIButton and set it to UIControlContentHorizontalAlignmentRight this will position the image you add to it to the far right.

Edit: As suggested by @danypata you can also use UIButton's property imageEdgeInsets to leave some margin around your 'accessoryView' like so:

[_addImage setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 10)];

Regarding having the text on the left and the image on the right, I believe it's not possible (someone please correct me). For that I would either create my own custom button by extending UIControl or add a UILabel and set it's frame (as opposed to using UIButton's setTitle:forState:) and add it as a subview of the button.

Hope this helps!

LuisCien
  • 6,362
  • 4
  • 34
  • 42
  • I am looking a way to insert a new image inside. – Jonathan Thurft Jun 11 '13 at 21:14
  • 1
    Yes, that is no problem. UIButton has two methods for this, one is 'setBackgroundImage:forState:' and the other one is 'setImage:forState:'. Thanks to this you can add two different images, one as the background and the other one as the 'accessory view' that you want. There will be no collision – LuisCien Jun 11 '13 at 21:17
  • Thanks. I eddited my question to ask for a bit more guidance look at the bottom – Jonathan Thurft Jun 11 '13 at 21:24
  • @LuisCien You should complete your answer by adding the content insets, because the `UIControlContentHorizontalAlignmentRight` is not enough. – danypata Jun 11 '13 at 21:32