简体   繁体   中英

How to make an image view and a label adaptive to each other in swift

I have a UI View, which consists of image view and a label. UI view has constraints set up to the superview. These labels are of different sizes,(data coming from backend) so I want to adjust the background image according to the length of the label.

For example : Label : "Hi How are you" In this scenario - the image should fit the label and should look proportionate to the screen

Label : "Hi" In the scenario - the image should fit the label containing just "Hi", meaning it should shrink based on the label string length.

Is this possible ?

Follow these steps:

  1. Take your UIView into a UIScrollView or tableviewcell or collectionviewcell.
  2. Give UIView constraint without height.
  3. Take your UIImageView and UILabel inside UIView(don't give UILabel height constraint).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM