简体   繁体   中英

How do I make a UIImageView be half the height of the UICollectionViewCell's height in Storyboard/Interface Builder

I'm confused as to how I'd set a UIImageView embedded in a UICollectionViewCell to half the height of this cell. When I try to create constraints between the two there doesn't appear to be a height option.

Is such a thing possible without delving into code?

I think it's possible. Should take about 1 minute to do.

Try the following:

  1. Add a UIView to your UICollectionViewCell (you may need to make this transparent and covering the entire cell depending on your requirements)
  2. Place the UIImageView inside the UIView (I recommend drag and drop in the Document Outline )
  3. In the Document Outline drag from the UIImageView to the UIView and choose Aspect Ratio
  4. Find the correct Aspect Ratio constraint in the Size Inspector and choose Select and Edit...
  5. Make sure the First Item is Image View.Height (or alter it to be this)
  6. Make sure the Second Item is SuperView.Height (or alter it to be this)
  7. Change the Multiplier to 1:2

Steps 5 and 6 may be the other way round in which case use Multiplier of 2:1

Enjoy your UIImageView resized to half your UICollectionViewCell ! You will need to set the height of your UICollectionViewCell elsewhere.

Update: I've just tested this in Xcode / IB and it works.

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