Sizing controls after rotation

I have an iOSMobiletable on screen.
I would like it to be a different width when the view is portrait versus landscape.
Other controls are set to be left= (this control’s right)
I was expecting to widen it and see all the other controls move accordingly

But the .width property is read-only (as is the .left etc)

Is there no way to do this?

Using a constraint?

Without seeing all the controls and their layout constraints, it’s difficult to tell what the best solution would be.

From your explanation, I would have two width constraints on the table: one for portrait and one for landscape. Give them both a name, like “widthPortrait” and “widthLandscape”.

In portrait mode you do this
self.constraint("widthPortrait").active = true
self.constraint("widthLandscape").active = false

In landscape mode it’s this
self.constraint("widthPortrait").active = false
self.constraint("widthLandscape").active = true

Having said that, I usually try to avoid working with width-constraints. I prefer left and right, but that’s just a preference.

2 Likes

I prefer left and right, but that’s just a preference.

Me too, but they don’t work in iOS
At the moment I don’t know what a ‘constraint’ is.
I’ll go see if I can work it out.

1 Like

Like this. The “widthLandscape” is inactive, that’s why it’s red. With the code from above it will become active when rotated to landscape and the “widthPortrait” will become inactive.

Interesting.
I can see no way to add named constraints to a control in that manner.
Im currently exeprimenting with view-level properties of iOSConstraint type, cosntructing them in the Open event, and making them active when I need to.
If you can apply these to a control in this way, why do we need to add them to the view?

Somebody correct me if I’m wrong but my understanding is that even if you see the constraints on a control in the IDE, Xojo always adds them to the MobileScreen.

You could do the exact same thing from above in code - except that I do not know how to set the “Name” in the code. You would have to use properties instead.

That’s it! Named constraints can only be used when adding constraints in the layout editor.
When adding constraints by code, keep a reference to them in a property to enable/disable them.

Well, the docs make no sense to me.
But I have managed to cobble something together.

The documentation talks about creating a new button in a method and adding contraints there.
(Nobody is ever going to do that.?)

I added a property to the view, of type iosLayoutConstraint, and initialised it in the Opening event

Constraint_portrait = new  iOSLayoutConstraint(mycontrol,iOSLayoutConstraint.AttributeTypes.width, iOSLayoutConstraint.RelationTypes.Equal, _
self, iOSLayoutConstraint.AttributeTypes.width,0.02,60)

me.AddConstraint Constraint_portrait

But the parameters are not explained, and don’t seem to match those visible in the IDE
‘addend’ ??

When setting a constraint at design time, its perfectly possible to say that there is no ‘relative control’
Eg you set ‘none’, and 100 percent, and (say) a value of 30

But in code that second control cannot be nil, so I had to put something in.
The last 2 parameters … just … ???
So the values above happen to work for me.
If I change that 60, the control changes size. Result.

On the control itself I have a ‘default’ width property.
I have it set as a fixed (say) 30 , and a low priority

When I want this other layout to take effect, I set it to Active=true and it overrides the default

Working for me.

But the screenshot above looks like a better way of doing things, even if I cannot work out what is going on there.