iOSLayoutConstraint ?

Can someone help me with Constraints please since Xojo samples can’t help me much…

’ View has one TextField control on it and one Button
’ On Button Action event I want to change Constraints of TextField control and bind left and right with standard gap and for left and for right
’ Left bind is done well but Right not, why ?

Dim xRight As New iOSLayoutConstraint(TextField1, _
iOSLayoutConstraint.AttributeTypes.Right, _
iOSLayoutConstraint.RelationTypes.Equal, _
Self, iOSLayoutConstraint.AttributeTypes.Right, _
1.0, iOSLayoutConstraint.StandardGap, 1000)

Dim xLeft As New iOSLayoutConstraint(TextField1, _
iOSLayoutConstraint.AttributeTypes.Left, _
iOSLayoutConstraint.RelationTypes.Equal, _
Self, iOSLayoutConstraint.AttributeTypes.Left, _
1.0, iOSLayoutConstraint.StandardGap, 750)

xRight.Active = True
xLeft.Active = True

// Disables the existing Width constraint for the TextField.
Dim widthConstraint As iOSLayoutConstraint = Self.Constraint(“TextFieldWidth”)
widthConstraint.Active = False
TextField1.RemoveConstraint(widthConstraint)

It seems you forgot to add the constraints to the view.

self.AddConstraint(xRight) self.AddConstraint(xLeft)

Hi Jeremie,

Thanks mate a lot for points.

Well that helps but it’s not a total working solution.

I played with it and figure it out that Right value isn’t good so in bellow code you have working solution.

If you say TextField1.AddConstraint(…) then app will crush in simulator (at me).

Maybe a code solution:

’ Remove current Width constraint named TextFieldWidth
Dim widthConstraint As iOSLayoutConstraint = Self.Constraint(“TextFieldWidth”)
widthConstraint.Active = False
TextField1.RemoveConstraint(widthConstraint)

’ Defin new constraint (Right)
Dim xRight As New iOSLayoutConstraint(TextField1, _
iOSLayoutConstraint.AttributeTypes.Right, _
iOSLayoutConstraint.RelationTypes.Equal, _
Self, iOSLayoutConstraint.AttributeTypes.Right, _
1.0, iOSLayoutConstraint.StandardGap, 1000)

’ Defin new constraint (Left)
Dim xLeft As New iOSLayoutConstraint(TextField1, _
iOSLayoutConstraint.AttributeTypes.Left, _
iOSLayoutConstraint.RelationTypes.Equal, _
Self, iOSLayoutConstraint.AttributeTypes.Left, _
1.0, iOSLayoutConstraint.StandardGap, 1000)

’ Make new constraints active
xLeft.Active = True
xRight.Active = True

’ Fix Offset part for Right constraint
xRight.Offset = -1 * xRight.Offset

’ Add constraints to constraints stack
Self.AddConstraint(xLeft)
Self.AddConstraint(xRight)

[quote=349814:@Bogdan Pavlovic]’ Fix Offset part for Right constraint
xRight.Offset = -1 * xRight.Offset[/quote]
That is quite strange. Using StandardGap the value should automatically be negative in the view.

Also be careful when using RemoveConstraint. I think there is a bug if the parent view is an iOSContainerControl. If your app crashes, better use

constraint.active = false

In my test case it’s crush if you put

TextField1.AddConstraint(xLeft)
TextField1.AddConstraint(xRight)

Rather then

Self.AddConstraint(xLeft)
Self.AddConstraint(xRight)

Constraints must always be added to the containing view/control.

So if you TextField is a child element of an iOSView, and you are creating the constraint in the iOSView.Open event for example, you need to set it like this:

self.AddConstraint

I know but I was experimenting with Xojo logic since I’m newbie to it.

Anyway bellow code is working so logically if RemoveConstraint is working then Add should works too? ,

Dim widthConstraint As iOSLayoutConstraint = Self.Constraint(“TextFieldWidth”)
widthConstraint.Active = False
TextField1.RemoveConstraint(widthConstraint)