Forum Archive

ui view anchor redefine constraints

Drizzel

How would I go about changing the constraints of a view? In the example code below I’m trying to redefine the constraints of slide_view when the menu button button is pressed, which (obviously) doesn’t work.

import ui
from anchor import *

if __name__ == '__main__':

    class Test_slide(ui.View):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.previous_size_class = None
            self.active_constraints = []
            enable(self)
            self.create_ui()

        def style(self, view):
            view.background_color = 'white'
            view.border_color = 'black'
            view.border_width = 1
            view.text_color = 'black'
            view.tint_color = 'black'

        def create_ui(self):
            slide_frame = View(name='Slide frame')
            slide_frame.show = True
            self.style(slide_frame)
            self.add_subview(slide_frame)
            slide_frame.dock.leading(share=.3)

            main_frame = View(name='Main frame')
            self.style(main_frame)
            self.add_subview(main_frame)
            main_frame.dock.fit()
            main_frame.dock.trailing()
            main_frame.at.leading == slide_frame.at.trailing

            def slide(sender):
                slide_frame.show = not slide_frame.show
                if slide_frame.show:
                    print('slide in')
                    slide_frame.dock.leading(share = .3)
                else:
                    print('slide out')
                    slide_frame.dock.leading(share=.0)

            menu_button = Button(name = 'menu button', title = 'Menu').dock.fit()
            self.style(menu_button)
            menu_button.action = slide
            main_frame.add_subview(menu_button)
            menu_button.dock.top_leading()


    root = Test_slide()
    root.present('fullscreen', hide_title_bar=True, animated=False)

The code is just an edited version of the example script in the anchor module by @mikael

Drizzel

Just as an addition, is it also possible to animate the change?

stephen

@Drizzel here are some "highlights".
these are what you have to work with in ui..


View.content_mode

Determines how a view lays out its content when its bounds change. Can be one of the View Content Mode constants listed below. For custom View subclasses, this is set to CONTENT_MODE_REDRAW by default.


View.flex

The autoresizing behavior of the view. When a view changes its bounds, it automatically resizes its subviews according to the flags set by this attribute.
The flags are represented as a string of one-letter “codes” that specify how the view should be resized.
Valid flags are “W” (flexible width), “H” (flexible height), “L” (flexible left margin), “R” (flexible right margin), “T” (flexible top margin), “B” (flexible bottom margin).

Examples:

  • “WH” – the view’s width and height are automatically adjusted, this is often suitable for views that fill the entire superview.
  • “TR” – the view is anchored to the bottom-left corner of its superview (the top margin and right margin are flexible).
  • If you don’t want auto-resizing to occur, use an empty string.

---

as far as animating it heres a peive from Docs about..

View Animation

Changes of a lot of View attributes can be animated. For example, you can animate the View.frame attribute to smoothly change its size and position, or View.alpha for fade-in/out animations.
First, you have to write a function that describes the changes you want to apply. This will often be an inner function. Then, you can pass a reference to this function to the animate() function, optionally passing a duration and delay for the animation:

import ui

# The button's action:
def button_tapped(sender):
    def animation():
        sender.alpha = 0.0 # fade out
    ui.animate(animation, duration=1.0)

# Set up and present a view with a single button:
v = ui.View(background_color='white')
button = ui.Button(title='Tap me!')
button.action = button_tapped
v.add_subview(button)
v.present('sheet')

---

so with all that said.. whatbare you trying to do exactly? the code you posted would need a complete rewrite to run but im sure a good ole story tike can getntjis rollin? 😊🤓

butbas far as constraints best bet is objc_utils or scene

Drizzel

Thanks a lot @stephen!
I was worried I’d have to rewrite everything, so I for now decided on a different ui layout that uses as few moving views as possible. I’ll play around a bit and, if I haven’t figured it out by then, get back to you.

stephen

@Drizzel 10-4 rubber ducky 🧑🏻‍💻😉

WeirdVulture

Thank you for the information and advice! mywegmansconnect