Hello, as a huge fan of auto positioning in vanilla, I am trying to figure out how to add UI elements to an auto-layouted view. I just cannot get it to work. The problem seems to be that I cannot “update” the auto positioning rules, only add to them. Take this script for example:
Clicking the first button adds a second button and attempts to update the auto positioning rules accordingly. This works for the horizontal rules, but not for the vertical ones – because the first rule is still applied, wherein the first button defines the vertical constraints of the window. The second button thus doesn’t fit.
Does anybody have any clue how to work with this? I don’t want to re-write everything with frame layout, that’s for amateurs
Any pointers as to where to find answers otherwise are also appreciated, I cannot find a resource apart from the vanilla docs, where this is not mentioned. Many thanks!
Consider using a VerticalStackView with just a single button and then adding the second button with appendView. I have not tried this, but it should still interoperate with any outside Auto Layout code.
Thanks a million! That works great. Not quite what I was looking for, but I was simply looking for the wrong thing (coding it all myself), instead of finding a pre-made solution right out of the box.
It’s theoretically possible to do the same by manually removing/deactivating layout constraints and then adding/activating them again when needed, but that is cumbersome and error-prone. Stack views do the same thing – they also use Auto Layout under the hood – just nicely packaged so that you don’t need to do all the bookkeeping yourself.
Could you tell me where I can find some documentation on how to update layout constraints? I am so far only using vanilla’s addAutoPosSizeRules but have scoured various documentations for signs of other methods in vain.
As far as I know, there is no nice way to do it with Vanilla; you have to drop down to the AppKit API and use its Auto Layout methods directly. Generally, I would advice against doing that. Even Apple heavily promotes using stack and grid views over doing the same using manual Auto Layout constraints. But, if you must, have a look at the Auto Layout Guide by Apple:
I was just curious, in order to be able to do it myself. But I’m having great fun with VerticalStackView, it works great. Thanks so much for your help!
Sorry to be following up on this again, but there seems to be a problem with callbacks – I added some buttons to my VerticalStackView, with a callback function defined. It doesn’t appear to be called anymore when clicking the buttons. In fact, nothing seems to be called anymore. Buttons, EditText, PopUpButtons, nothing works. Is there something that needs to be done especially?
Buttons outside of the stack view still work.
What’s more, all my PopUpButton lists within the stack view are greyed out.
Thanks! This is indeed the problem. Sadly, this only solves my issue to an extent, as only the button added last is linked to the given callback. So if I instead use
for number in ["one", "two"]:
group = Group("auto")
self.button1 = Button("auto", "Button", callback=self.button_callback)
only the second button triggers the callback. Can you think of a method to accomodate this?