Setting Top Edges of Controls Relative to the Top of the View

OK, so continuing my saga of learning how to apply iOS constraints…

Here’s what I want to do.

I want to dynamically create a number of say canvases.

I want the top of each canvas to be spaced relative to the top of the view and shifted by say 100 pixels * whatever the index of the canvas is going to be. So for example, the first canvas being index 0 would have it’s top at 0, the second at 100, the third at 200, etc.

So I have this constraint:

    Dim ctop as New iOSLayoutConstraint(c, _
    iOSLayoutConstraint.AttributeTypes.Top, _
    iOSLayoutConstraint.RelationTypes.Equal, _
    self, _
    iOSLayoutConstraint.AttributeTypes.Top, i ,100.0)

But I was only drawing a single control. So I’m thinking about this and then it dawned on my that the Top of the view is always going to be 0. So if the value of “i” is always going to be multiplied by 0, then it’s always going to place every control 100 pixels down.

So instead, is something like this more appropriate?

    Dim ctop as New iOSLayoutConstraint(c, _
    iOSLayoutConstraint.AttributeTypes.Top, _
    iOSLayoutConstraint.RelationTypes.Equal, _
    self, _
    iOSLayoutConstraint.AttributeTypes.Top, 1 ,100.0*i)

Or is there a better way to do this?

Wouldn’t you make each canvas relative to the previous one? That way, if you want to remove one, you can inspect the one you are removing and create a single new constraint for the one below to move all of them up?

Greg - ideally that’s what I’d like to do. But I wasn’t sure yet how to reference the previous control. And I wasn’t sure how to handle the very first one. I suppose I could put in a conditional statement in the For/Next loop where I create these to constrain the first canvas to the edge of the view and then succeeding canvases to the one before it. Is storing the controls in an array and then referencing the previous control from the array the right way to do that?

You may want to use the top layout guide (which is very close to the top of the view)

Norman,

Is TopLayoutGuide and BottomLayoutGuide even in the docs? I did a search for them and they don’t show up. But yet, they show up as properties of the view. I look in the iOSView documentation and they are not listed as properties.

Where can I find documentation on these?

And actually, I’m not quite sure how you can use that in a constraint…

ctop = New iOSLayoutConstraint(c, _ iOSLayoutConstraint.AttributeTypes.Top, _ iOSLayoutConstraint.RelationTypes.Equal, _ self, _ iOSLayoutConstraint.AttributeTypes.Top, 1 ,(100.0*i)+10)

So TopLayoutGuide is not an attribute I can use as a reference point. I have to use Top.

not sure - I dont write the docs but do put in notes on cases I work on indicating things need to be documented

you should be abel to do something like

ctop = New iOSLayoutConstraint(c, _ iOSLayoutConstraint.AttributeTypes.Top, _ iOSLayoutConstraint.RelationTypes.Equal, _ toplayoutguide, _ <<<<<<<<<<<<<<<<<<<<<<<<<< iOSLayoutConstraint.AttributeTypes.Bottom, 1 ,(100.0*i)+10) <<<<<<<<<<<<<<<<

[quote=203749:@Norman Palardy]not sure - I dont write the docs but do put in notes on cases I work on indicating things need to be documented

you should be abel to do something like

ctop = New iOSLayoutConstraint(c, _ iOSLayoutConstraint.AttributeTypes.Top, _ iOSLayoutConstraint.RelationTypes.Equal, _ toplayoutguide, _ <<<<<<<<<<<<<<<<<<<<<<<<<< iOSLayoutConstraint.AttributeTypes.Bottom, 1 ,(100.0*i)+10) <<<<<<<<<<<<<<<<[/quote]

Yeah, I just realized that - since TopLayoutGuide is an object I can use it as the reference point. Was about to edit/remove my post when yours came in!

Now just to have Paul fix those docs! I’ll file a bug report…

Feedback report on the Top/Bottom LayoutGuides:

<https://xojo.com/issue/40273>