Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40156

iOS | How to keep overlay label width same to actual image rather to imageview in XCode

I have an imageview whose view mode is set to Aspect Fit. I have also a label on the middle of the imageview. I can set label width constraints to imageview but it looks odd when image aspect changes on runtime.

Label width is showing fine

In this above portrait mode, at the middle of the image, the overlay text label width is same with the actual image width.

enter image description here

But when it's rotated, due to constraints, the ratio width becomes different and hence the width of the label.

My question is is there any way to set constraints from storyboard so that the label width will always be same with the actual image width at runtime, no matter what the imageview width becomes. Or is it necessary to handle by source code. If so, can anyone provide a short guide.

Upvotes: 0

Views: 754

Answers (1)

Mahendra
Mahendra

Reputation: 8914

To set the width to label same to actual width of a image, you need to do it programatically.

You need to set dynamic width constraint of a label and set constant with image.size.width .

It will set actual image's width to label.

Upvotes: 3

Related Questions