ios - How to make a square view resize with its superview using auto layout -


in particular case have view inside view controller added following constraints:

  • set leading, trailing, top , bottom edges 0
  • set multiplier bottom edge 2:1

now view sits in top half on view controller.

inside add square image view, added following constraints:

  • ctrl drag image view superview , added equal heights , widths.
  • change multiplier width , height until have perfect square.
  • added constraints center vertically , horizontally

my constraints seems perfect, when running in simulator don't perfect square. besides this, image view doesn't resize when running on different simulator screens.

this setup:

  • auto layout , size classes enabled
  • i use inferred size storyboard
  • adaptive layout set width , height
  • i trying run 4s,5,6 , 6+ simulators.

i looked on other stackoverflow posts, nothing seems work.

are there basic steps ?

enter image description here

edit:

after setting >=10 constraints:

enter image description here

edit 3: added top,bottom,leading,trailing constraints 2 times, 1 lower or equal(priority 1000), other 1 greater or equal(priority 800) constant value of 90. don't know why bottom tries streches 90 pt main view, not container view(green one).

enter image description here

you have view needs expand fill container while maintaining aspect ratio. common pattern auto layout.

the trick use 2 constraints leading/trailing/top/bottom:

  • =10 @ low priority

  • >=10 @ required priority.

putting together, have:

  • aspect ratio 1:1

  • center x/y in superview

  • leading/trailing/top/bottom superview = 10 (at 750 priority)

  • leading/trailing/top/bottom superview >= 10 (at 1000 priority)

there couple of things consider uiimageview:

  • uiimageview have intrinsic content size based on image displaying, you'll want ensure content hugging priority lower 750 priority use =10 constraints.

  • uiimageview.contentmode determines how underlying image sized relative size of uiimageview. default, it's set uiviewcontentmodescaletofill.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -