I am trying to put my subview with a left margin based on the width of the parent view. It sounds simple, but I can't figure out how to do this using autorun.
Logically, I would need to set the left margin value with a certain percentage value of the parent width, but at the moment I can not translate this logic into auto-detection.
This is my code at the moment:
var view = UIView(); view.backgroundColor = UIColor.redColor(); view.frame = CGRectMake(0, 0, 320, 400); var sview = UIView(); sview.setTranslatesAutoresizingMaskIntoConstraints(false); sview.backgroundColor = UIColor.yellowColor();
Here the code returns an error:
var con2 = NSLayoutConstraint(item: sview, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 0.75, constant: 0.0); view.addConstraint(con2);
Error:
* Application termination due to the uncaught exception 'NSInvalidArgumentException', reason: '* + [NSLayoutConstraint constraintWithItem: attribute: relatedBy: toItem: attribute: multiplier: constant]: Invalid mate attributes pairing
Does anyone have any ideas on how to achieve this? I just want the left margin to be 0.75% of the width of the parent's view.
Thanks.
source share