bitbucket158
As a way of switching between views, I'm creating a top-level root view object and then adding/removing child views to/from that. The child views are those created with the layout editor and loaded using ui.load_view().
What I'm finding is that if I load a view and present it, everything works fine. The view fills the screen on my iPhone 4s. If, however, I create the root view object, then add the loaded view to that and present the root view, it does not fill the screen.
I've experimented with setting the flex attribute on the root view but this didn't seem to help (I tried 'WH' for example).
Here's some sample code which illustrates the issue.
import ui v = ui.load_view('test') mode = 0 if mode == 0: v.present() else: rootView = ui.View() rootView.add_subview(v) rootView.present()
The above code is in test.py and there's a simple test.pyui to go along with it. In the above, I'm manually toggling the mode variable value to test things. If mode == 0, it works correctly and the UI fills the screen. If mode == 1 it does not fill the screen.
Any suggestions?
bitbucket158
The above worked great for a single subview. I then tried removing the first subview and adding a second (as a way to switch between views).
The second view exhibited the original problem, it didn't fill the screen.
I found the solution here was to set the width and height of the second subview to match that of the root view. I assume additional views will also need this.
bitbucket158
I found a workaround which seems to have addressed the issue.
Instead of creating the root view programmatically, I simply created a new 'script with ui' left it empty and named it 'RootView'. I then load this and set the child view (also loaded) flex to 'WH'.
Here's the updated code.
import ui v = ui.load_view('test') mode = 1 if mode == 0: v.present() else: rootView = ui.load_view('RootView') v.flex = 'WH' rootView.add_subview(v) rootView.present()
So there must be something different when loading an empty view vs creating it programmatically which causes it to fill the screen.
This only requires creating the extra dummy root view ui script but I think provides a nice workaround.
bitbucket158
I created a label in the center of the view and set the auto resizing parameter to WH.
It works fine if I present the loaded view directly. It doesn't work if I add it to the root view.
Any other ideas?
bitbucket158
I modified the code using your suggestion. When I run with those mods the ui is too large. It extends beyond the bottom of the screen.
I'm using an iPhone 4s.
bitbucket158
I'm using Pythonista on an iPhone. The docs indicate that in that context fullscreen is the default. I did try adding the fullscreen arg though just for kicks but it didn't make a difference.
I also tried setting the rootView size to match the subview as well as calling size_to_fit but these didn't make any difference.
bitbucket158.
bitbucket158
Thank you! I was previously unable to find remove_subview in the docs, but I went back again and see it there. Thanks for the help :)
bitbucket158
You can switch between sub views using the View add_subview and remove_subview methods.
So you could start with subview 1 active then remove it and add subview 2 to replace it.
bitbucket158
omz, how do I "replace the subview with one you've loaded from elsewhere"? The docs indicate that the subviews attribute of the View class is a readonly tuple. So how do I go about replacing a subview with another one?