3

I am new in autolayout (I am good in autoresizing). My requirement is:-

  1. I have 2 view (say blue and green, width and height of both view is 300 pixel in iPhone5). height and width of both view can change according to screen resolution. X and Y origin can also change. 2. In autoresizing I am using this mask to both views for my requirement.

  2. What constraints should I use for this autoresizing masks.

Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • Set all 4 directions constraints such as top space to superview to 0. – gabbler Nov 12 '14 at 07:25
  • Uh...so blue and green square views are stacked vertically without any space in between, they're both 300 pixels width and height. It doesn't matter what value of X and Y the blue box is, as long as blue and green box are always together ? Do you want both squares to be centered to the screen ? – Zhang Nov 12 '14 at 08:28
  • I got a solution here.. http://stackoverflow.com/questions/28513625/autolayout-origin-and-size-should-change-according-to-width-and-height-factor – Mayank Jain Mar 02 '15 at 08:12

1 Answers1

1

It is unclear if the blue and green are siblings or if one contains the other.

If the effect you want to achieve can be produced through an autoresizing mask, then you can set translatesAutoresizingMaskIntoConstraints to true and the autoresizing mask will be automatically translated into constraints.

In general, if you want to install constraints manually that mimic the effect of an autoresizing mask with a flexible width and flexible height, then you would do this by creating four constraints maintaining a fixed distance between the edges of the view and the corresponding edges of its superview -- that is, the top of the view and the top of the superview, the bottom with the bottom, the left with left, and right with right.

This could be done with the visual format language with strings like "H:|-(10)-[view]-(10)-|" and "V:|-(10)-[view]-(10)|". This assumes there are no other conflicting constraints that try to set the height and width of the view.

algal
  • 27,584
  • 13
  • 78
  • 80
  • Where can I find translatesAutoresizingMaskIntoConstraints option in storyboard? – Mayank Jain Nov 12 '14 at 07:50
  • Seems like there's no control in Interface Builder for it for iOS (http://stackoverflow.com/questions/17018533/where-is-the-xcode-checkbox-translates-autoresizing-mask-into-constraints). So you need to set it in code. Set it in your view controller's viewDidLoad, or subclass the view and set it in awakeFromNib. – algal Nov 12 '14 at 07:53
  • before doing this, should I disable autolayout from my IB? – Mayank Jain Nov 12 '14 at 08:01
  • No. translatesAutoresizingMaskIntroConstraints uses Autolayout. It just lets you use the old autoresizing mask interface to generate Autolayout constraints. – algal Nov 12 '14 at 08:04