Pinning a container width to the device screen width

How do I change a container width so that it is the same size as the device screen (or the scrollable area it sits in) ?
I’ve tried using constraints but it seems to ignore them. I don’t understand why the Container object doesn’t have it’s own auto-layout controls in the inspector, or at least a toggle to fix its width to its parent - this must be a very common requirement as there are very few situations where you would want controls slip-sliding left/right in the UI.

For clarity, I have a Screen called “MainScreen”
A scrollable area called “sa”
and a Container called “Main”

I have the auto-layout features working fine within MainScreen
The individual fields, labels and buttons are similarly set-up with auto-layout in Main, but TBH this was a waste of time unless Main resizes to fit the available width on the device.

Any advice would be welcome please.

You must use constraints in Xojo. Select the instance that’s on the view and make sure you have a left constraint that’s set to the left edge of the view with an offset of 0 and either a width constraint set to the width of the view or a right constraint that’s set to the right edge of the view with a 0 offset, but not both.

Generally, the rule is to have exactly two “equals” constraints per control per direction.

Left and (width or right)
Right and (width or left)
Top and (height or bottom )
Bottom and (height or top)

It is possible to add more with lower priorities to deal with more complex situations if necessary, but generally, two is the rule.

I get the idea, and I have successfully applied this to individual controls. I just can’t seem to get it to work with my container. I also found a few threads here that I thought might help but the code just isn’t working and throwing errors, or doing nothing at all

I got there in the end. Final code was placed in the sa (scrollable area) Opening event:

Var cons As New iOSLayoutConstraint( sa.container, _
iOSLayoutConstraint.AttributeTypes.Width, _
iOSLayoutConstraint.RelationTypes.Equal, _
Self, _
iOSLayoutConstraint.AttributeTypes.Width, _
1.0, 0, 1000)

Self.AddConstraint(cons)

2 Likes