iOSView.AddControl() conundrum

I have been trying to understand the new way to dynamically add a control to a view in iOS, but the Zenish sobriety of the LR is beyond me :

AddControl(child As iOSControl) Adds a control to the view.

I tried this but nothing happens.

dim b as new iOSButton self.AddControl(b)

Besides, in Desktop or Web Edition, after popping up a new control, I would set left, top, height, width. How am I supposed to do so with all the Autolayout stuff ?

Help, please.

Look at the dynamic controls example project. Basically you have to create the control, create and add all the AutoLayout guides, then add the control. Its not quite as easy as we are led to believe.

I just succeeded adding a textfield, but adding constraints is not a piece of cake.

I was looking for an example in the wrong place. I finally located DynamicControls.xojo_binary_project and LayoutConstraintExample.xojo_binary_project which maybe both will help.

Thank you Jason.

I made a small extension method. Call it like:

dim b as new iOSButton b.Caption = "Hello" self.AddControlAtPoint(b,new xojo.Core.Point(25,100))

Enjoy!

Sub AddControlAtPoint(extends mView as iOSView, ctrl as iOSControl, pt as xojo.Core.Point)
  Dim left As New iOSLayoutConstraint(ctrl, _
  iOSLayoutConstraint.AttributeTypes.Leading, _
  iOSLayoutConstraint.RelationTypes.Equal, _
  mView, _
  iOSLayoutConstraint.AttributeTypes.Leading, _
  1, _
  pt.x)
  mView.AddConstraint(left)
  
  Dim top As New iOSLayoutConstraint(ctrl, _
  iOSLayoutConstraint.AttributeTypes.Top, _
  iOSLayoutConstraint.RelationTypes.Equal, _
  mView, _
  iOSLayoutConstraint.AttributeTypes.Top, _
  1, _
  pt.y)
  mView.AddConstraint(top)
  
  mView.AddControl(ctrl)
End Sub

[quote=151899:@Jason King]I made a small extension method. Call it like:

dim b as new iOSButton b.Caption = "Hello" self.AddControlAtPoint(b,new xojo.Core.Point(25,100))

Enjoy!

[code]
Sub AddControlAtPoint(extends mView as iOSView, ctrl as iOSControl, pt as xojo.Core.Point)
Dim left As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.Leading, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.Leading, _
1, _
pt.x)
mView.AddConstraint(left)

Dim top As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.Top, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.Top, _
1, _
pt.y)
mView.AddConstraint(top)

mView.AddControl(ctrl)
End Sub
[/code][/quote]

Hey, that is neat ! Thank you Jason :slight_smile:

You’re welcome! Glad I could help. Here is another to use with controls that have a more variable size (i.e. rectangles, ovals, etc)

[code]Sub AddControlWithBounds(extends mView as iOSView, ctrl as iOSControl, bounds as xojo.Core.Rect)
Dim left As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.Leading, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.Leading, _
1, _
bounds.left)
mView.AddConstraint(left)

Dim top As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.Top, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.Top, _
1, _
bounds.top)
mView.AddConstraint(top)

Dim right As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.Right, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.left, _
1, _
bounds.right)
mView.AddConstraint(right)

Dim bottom As New iOSLayoutConstraint(ctrl, _
iOSLayoutConstraint.AttributeTypes.bottom, _
iOSLayoutConstraint.RelationTypes.Equal, _
mView, _
iOSLayoutConstraint.AttributeTypes.top, _
1, _
bounds.bottom)
mView.AddConstraint(bottom)

mView.AddControl(ctrl)
End Sub
[/code]
Use like:

dim r as new iOSRectangle r.FillColor = color.Blue self.AddControlWithBounds(r,new xojo.Core.Rect(50,50,50,50))

Hum. Problem. It crashes. Apparently in the method.

It is in the AddConstraint part :frowning:

Which one? They both work for me…

I have had issues with AddConstraint as well. I haven’t gone back to look at it yet, though.

Add the control THEN add the constraints

The example project needs fixing then. It shows adding the constraints first…

I have done that, it fixes the problem :slight_smile:

I will try to add width and height, then it should be just as simple as what we have in Desktop :slight_smile: