在UIButton中添加一个图像作为附件

我有以下的UIButton我想添加一个图像里面到最右边。 像一个accessoryView

我已经使用backgroundImage,我想避免将2个图像合并为1。

是否有可能在UIButton中添加另一个图像是一个AccesoryView可以说一个图像有一个PLUS在最右边? (我正在寻找一种方法来在button内插入新的图像)

在这里输入图像说明

@luisCien评论

我试过了

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

看起来像这样,我想要在文本的右侧形象。 在这里输入图像说明

只需将其添加为子视图,然后select您的坐标。尝试如下所示:

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

你可以使用UIButtoncontentHorizontalAlignment属性,并将其设置为UIControlContentHorizontalAlignmentRight这将把你添加的图像定位在最右边。

编辑:如@danypatabuild议,你也可以使用UIButton的属性imageEdgeInsets在你的'accessoryView'周围留下一些空白,如下所示:

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

关于在左边的文字和在右边的图像,我相信这是不可能的(有人请纠正我)。 为此,我要么创build我自己的自定义button,通过扩展UIControl或添加一个UILabel并设置它的框架(而不是使用UIButtonsetTitle:forState: ,并将其添加为button的子视图。

希望这可以帮助!