1

I'm trying to resize my iOS app for any screen dimension with auto layout constraints programmatically, but I don't understand why this specific constraint doesn't work as I expected it to.

I think that the bottom of view1 doesn't fit as it should with top of view2 in this constraint, but I don't understand why ,because if I try this on a new project it works perfectly:

let verticalSpacing2 = NSLayoutConstraint(item: view2, attribute: NSLayoutAttribute.Top, 
    relatedBy: NSLayoutRelation.Equal, toItem: view1, attribute: NSLayoutAttribute.Bottom,
    multiplier: 1, constant: 10)

self.addConstraint(verticalSpacing2)
Gabriela
  • 11
  • 3
  • Your constraints say `view1.top = view2.bottom + 10`. Is this what you want? If you want `view1.bottom = view2.top - 10` then you need to switch the `attributes` and make the `constant` -10 (negative). – esh Nov 27 '15 at 12:56
  • Try Manual Layout, it is easier than auto Layout.. – B.Saravana Kumar Nov 27 '15 at 13:02
  • 3
    Manual layout is not easier and auto layout is now a vital part of iOS development. The easiest way is with Interface Builder but if you have to do it programmatically then check that there are no other constraints that are conflicting with this one. Remember that if you don't set constraints in IB then they are inferred and created by the system. – Rob Sanders Nov 27 '15 at 13:31
  • I suggest that you read [Debugging Tricks and Tips](https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/DebuggingTricksandTips.html) from Apple first – Code Different Nov 27 '15 at 16:45
  • If you add constraints programmatically there might be some chances you miss some of the required constraints. e.g. If you have three views and you want them to auto-layout with equal height width you need 1: On top view: top, left, right, bottom (to 2nd view), 2: On middle view left,right, bottom (Dont add top constrain as on view 1 you have assigned). 3: On bottom view left right and bottom constrains. – Saqib Omer Dec 02 '15 at 14:10
  • Finally,I realised which was the problem. Well,I taught that my views already have a dimension, but when i set the translateautoresizingmask to false, I guess their width and height were gone. I didn't see that because the views were covered ..So that's why my constraints didn't worked. Thank you guys ^_^ – Gabriela Dec 15 '15 at 08:07

0 Answers0