Hello everyone!
I’m wondering if there is a system like Autolayout on iOS? Or some libraries with the same function?
(Autolayout - system, which updates positions and sizes of views automatically depending on algebraic constraints between them)
Hello everyone!
I’m wondering if there is a system like Autolayout on iOS? Or some libraries with the same function?
(Autolayout - system, which updates positions and sizes of views automatically depending on algebraic constraints between them)
There is not. You can use relative placement to give you the ability to move one thing and move a whole bunch:
local buttonPadding = 5
local image1 = display.newImageRect(…)
image1.x = display.contentCenterX
image1.y = 50
local image2 = display.newImageRect(…)
image2.x = display.contentCenterX
image2.y = image1.x + image1.height * 0.5 + image2.height * 0.5 + buttonPadding
That will cause image2 to always be “buttonPadding” pixels away from the button above it. If you need to move the whole bunch of buttons up or down, just change the .y of the first one. Change the sizes of the images, it adapts as it goes. Of course you could do similar math on the .x as well.
Rob
Haven’t tried it myself, but there’s this: cassowary
I posted some of my own helpers a little while back on the Code Exchange, too.
UPDATE : Looks like the cassowary author will be presenting his layout engine, SILE (which appears to use cassowary behind the scenes), tomorrow at the FOSDEM conference, so if videos go up soon after you could probably see it in action.
To Rob: thanks for answer! I already use your solution but it has a disadvantage: if your object’s properties change, you have to manually move other object. Advantage of autolayout in iOS is that you just set up constraints and it is almost always enough: autolayout engine takes care of actual moving and resizing. To StarCrunch: I looked at your code, maybe I’ll use it for my project. Thanks!
Everything should be dynamic. If the object’s height changes, on the next screen update, it will use the new height and should auto layout.
Rob
There is not. You can use relative placement to give you the ability to move one thing and move a whole bunch:
local buttonPadding = 5
local image1 = display.newImageRect(…)
image1.x = display.contentCenterX
image1.y = 50
local image2 = display.newImageRect(…)
image2.x = display.contentCenterX
image2.y = image1.x + image1.height * 0.5 + image2.height * 0.5 + buttonPadding
That will cause image2 to always be “buttonPadding” pixels away from the button above it. If you need to move the whole bunch of buttons up or down, just change the .y of the first one. Change the sizes of the images, it adapts as it goes. Of course you could do similar math on the .x as well.
Rob
Haven’t tried it myself, but there’s this: cassowary
I posted some of my own helpers a little while back on the Code Exchange, too.
UPDATE : Looks like the cassowary author will be presenting his layout engine, SILE (which appears to use cassowary behind the scenes), tomorrow at the FOSDEM conference, so if videos go up soon after you could probably see it in action.
To Rob: thanks for answer! I already use your solution but it has a disadvantage: if your object’s properties change, you have to manually move other object. Advantage of autolayout in iOS is that you just set up constraints and it is almost always enough: autolayout engine takes care of actual moving and resizing. To StarCrunch: I looked at your code, maybe I’ll use it for my project. Thanks!
Everything should be dynamic. If the object’s height changes, on the next screen update, it will use the new height and should auto layout.
Rob