Add an image inside UIButton as an accesory

11,289

Solution 1

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];

Solution 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!

Share:
11,289

Related videos on Youtube

Jonathan Thurft
Author by

Jonathan Thurft

Updated on June 04, 2022

Comments

  • Jonathan Thurft
    Jonathan Thurft almost 2 years

    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

  • Jonathan Thurft
    Jonathan Thurft almost 11 years
    I am looking a way to insert a new image inside.
  • LuisCien
    LuisCien almost 11 years
    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
  • Jonathan Thurft
    Jonathan Thurft almost 11 years
    Thanks. I eddited my question to ask for a bit more guidance look at the bottom
  • danypata
    danypata almost 11 years
    @LuisCien You should complete your answer by adding the content insets, because the UIControlContentHorizontalAlignmentRight is not enough.