Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Top and TopMargin in NSLayoutAttribute?

I am trying to create a constraint via code:

constraintImageCharacterTop = NSLayoutConstraint (item: image,
        attribute: NSLayoutAttribute.Top,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.Top,
        multiplier: 1,
        constant: viewTop)
    self.view.addConstraint(constraintImageCharacterTop)

However, I am not sure which is the right NSLayoutAttribute for this constraint. image should have a top space to the main Superview self.view.

This is how I think it works, but I am not sure if I am correct:

Imgur

Should I use NSLayoutAttribute.Top or NSLayoutAttribute.TopMargin for image A?

like image 935
Cesare Avatar asked Feb 01 '15 10:02

Cesare


1 Answers

I usually don't use constraint to margin, it is a personal preference, either to constraint to the edge of the view or to constrain to its margin (8px). Let's say your Image A has a top of 8px, you could create a top constraint with a constant of 8, or a top margin constraint with a constant of 0, you will get the same result.

like image 122
Jad Feitrouni Avatar answered Oct 03 '22 06:10

Jad Feitrouni