Add constraint to UITextField programmatically

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Add constraint to UITextField programmatically

mobiledev1600
I have a UITextView added programmatically in a tableview. I would like to add constraint to it so it will look same in portrait and landscape mode. I have following code(C#, MonoTouch) but it doesnot work. titleTxt is the simple textfield added to the cell.

var rightSpaceConstraint = NSLayoutConstraint.Create (titleTxt, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, null, NSLayoutAttribute.Right, 1.0f, 20f);
var leftSpaceConstraint = NSLayoutConstraint.Create (titleTxt, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, null, NSLayoutAttribute.Left, 1.0f, 20f);

titleTxt.AddConstraints (new NSLayoutConstraint[] {rightSpaceConstraint, leftSpaceConstraint});

How should i add constraint to set left and right spaces.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Add constraint to UITextField programmatically

spcware
you need to add the constraints to the parent view of the text view and make sure the auto resizing is not being translated into extra constraints. there is a flag to turn that off. it is on by default.

___
Shawn

> On Oct 9, 2013, at 1:33 PM, mobiledev1600 <[hidden email]> wrote:
>
> I have a UITextView added programmatically in a tableview. I would like to
> add constraint to it so it will look same in portrait and landscape mode. I
> have following code(C#, MonoTouch) but it doesnot work. titleTxt is the
> simple textfield added to the cell.
>
> var rightSpaceConstraint = NSLayoutConstraint.Create (titleTxt,
> NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, null,
> NSLayoutAttribute.Right, 1.0f, 20f);
> var leftSpaceConstraint = NSLayoutConstraint.Create (titleTxt,
> NSLayoutAttribute.Leading, NSLayoutRelation.Equal, null,
> NSLayoutAttribute.Left, 1.0f, 20f);
>
> titleTxt.AddConstraints (new NSLayoutConstraint[] {rightSpaceConstraint,
> leftSpaceConstraint});
>
> How should i add constraint to set left and right spaces.
>
>
>
> --
> View this message in context: http://monotouch.2284126.n4.nabble.com/Add-constraint-to-UITextField-programmatically-tp4658505.html
> Sent from the MonoTouch mailing list archive at Nabble.com.
> _______________________________________________
> MonoTouch mailing list
> [hidden email]
> http://lists.ximian.com/mailman/listinfo/monotouch
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Add constraint to UITextField programmatically

mobiledev1600
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Add constraint to UITextField programmatically

mobiledev1600
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.
Loading...