I have a UIButton with size of 200*200 and I have a image with size of 50*50. I want to horizontally align the image inside imageview, so that image occupy a frame of (150,75,50,50)
. I tried [button.imageview setContentMode:UIViewContentModeRight]
but this does not work. How can I align image ?

- 1,444
- 13
- 33
-
Basically I want image to be in the right side of imageview, irrespective of imageview's frame. – Jaffer Sheriff Jun 01 '16 at 11:53
-
try `imageview.contentMode = UIViewContentModeTopRight;` – Sunil Sharma Jun 01 '16 at 11:57
-
`button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;` did the trick – Jaffer Sheriff Jun 01 '16 at 12:37
4 Answers
I know it's been a while since February, but I just encountered the same need in the app I am developing.
I solved it using a custom UIImageView which can be easily integrated into your existing code (it's a drop-in replacement of UIImageView).
You can find the class on github, along with an example: https://github.com/reydanro/UIImageViewAligned
Hope this helps you on your project.

- 334
- 1
- 11
-
1
-
One solution is to subclass a UIView containing an UIImageView and change its frame according to image size. For example, you can find one version [http://stackoverflow.com/questions/3272335/alignment-uiimageview-with-aspect-fit]. – Kuldeep Singh Jun 01 '16 at 12:32
You can get size of the image so programmatically adjust the frame according to you need. In your case x position+width should the width of the view there you need to keep image.

- 288
- 2
- 13
You can manage this scenario something like this,
Take UIView
of size 200x200
.
Then add imageview of size 50x50
to that view with frame (150,75,50,50)
.
And set that image to this imageview. so, your outer view have desired blank space and your imageview fully filled by image and it will looks like image view having image set at right side.
hope this will help :)

- 27,092
- 9
- 50
- 75