layoutuiviewuikitsubview

Is it possible to create a bound between 2 views so their z-index is always next to each other?


Is there a way for me to have two views that always remain one on top of the other no matter what gets added?

For example I want to have ViewA contains ViewA1 and ViewA2. and if I decide to add a viewA3 below ViewA1 it won't be added right below it but below the next that is not "constrained" to be below ViewA1.

So ViewA1 and ViewA2 need to always be together and if I try to add a ViewA3 under ViewA1 it will go under ViewA2 instead.

Sort of like a constraint on the z axis between views. I've tried observing for zPositions, tried to observe changes on the subviews of the superview. No success yet. Any tips are welcome!


Solution

  • I've managed to find a working solution for this — it's possible to observe my superview.layer.sublayers for any changes. Then it's just a matter of making sure the layer of my viewA1 is always near the layer of viewA2.