Best practice when using SegmentedControl to switch visible subviews
eric last edited by
I want to use a SegmentedControl to switch between to two overlapped customview subviews. What is the best practice for this?
I tried to set the .hidden attribute in the SegmentedControl .action callback but this seems to work only when set before presenting the view.
Any advice would be wellcome.
JonB last edited by
If the views are completely overlapped, you could use bring_to_top or send_to_back. I am a little surprised hidden didnt work... are you sure you are targetting the right subviews?
Another approach would be to add_subview/remove_subview, but you would need to have to subview variables as global or instance vars of a custom Class.
omz last edited by omz
hiddenattribute should work for this.
Here's a minimal example that uses a segmented control to switch between a blue and a green view:
import ui def segment_action(sender): view2 = sender.superview['view2'] view2.hidden = (sender.selected_index == 0) main_view = ui.View(frame=(0, 0, 400, 400), bg_color='white') view1 = ui.View(frame=(0, 40, 400, 360), bg_color='blue', name='view1', flex='WH') view2 = ui.View(frame=(0, 40, 400, 360), bg_color='green', name='view2', flex='WH') main_view.add_subview(view1) main_view.add_subview(view2) control = ui.SegmentedControl(frame=(10, 4, 380, 32), flex='W') control.segments = ['View 1', 'View 2'] control.selected_index = 1 control.action = segment_action main_view.add_subview(control) main_view.present('sheet')
eric last edited by eric
Thanks for your reply, it does actually work. I don't know what I did first, I must have had a typo somewhere!
Using the (add/remove)_subview does the work to.