Rather than adding constraint to titleTxt you have to add the constraint to
table cell. Also you have to set the
TranslatesAutoresizingMaskIntoConstraints flag to false. Below is the code
that works fine
UILabel titleLbl = new UILabel ();
titleLbl.Frame = new RectangleF (20,5,260,30);
titleLbl.Text = "Please enter username";
titleLbl.BackgroundColor = UIColor.Clear;
titleLbl.Font = UIFont.SystemFontOfSize (13);
titleLbl.TextAlignment = UITextAlignment.Center;
titleLbl.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
titleLbl.TranslatesAutoresizingMaskIntoConstraints = false;
cell.ContentView.AddSubview (titleLbl);
var titleLblRightSpaceConstraint = NSLayoutConstraint.Create
(titleLbl, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal,
cell.ContentView, NSLayoutAttribute.Right, 1.0f, -20f);
var titleLblLeftSpaceConstraint = NSLayoutConstraint.Create
(titleLbl, NSLayoutAttribute.Leading, NSLayoutRelation.Equal,
cell.ContentView, NSLayoutAttribute.Left, 1.0f, 0f);
cell.ContentView.AddConstraints (new NSLayoutConstraint[]
{titleLblRightSpaceConstraint, titleLblLeftSpaceConstraint });
mobiledev1600 wrote
> Do you mean the auto resizing has to be off while adding constraint? In my
> case the parent view will be table view and if i add that like
> var rightSpaceConstraint = NSLayoutConstraint.Create (titleTxt,
> NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, tableView,
> NSLayoutAttribute.Right, 1.0f, 20f);
>
> Then is crashes. I guess thats why it is not applying correctly.
--
View this message in context:
http://monotouch.2284126.n4.nabble.com/Add-constraint-to-UITextField-programmatically-tp4658505p4658508.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch