Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
ui - root view does not fill the screen
-
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?
-
Try present('fullscreen')
Your view created in the designer has a width and height attribute already set, while
rootview
doesn't.
Alternatively, you can set width and height of rootview to whatever you want, namely the size of v.
PossiblyrootView.size_to_fit()
would also work. -
I'd recommend setting
v
's frame to the root view's bounds and settingflex
toWH
before adding it as a subview:import ui v = ui.load_view('test') rootView = ui.View() v.frame = rootView.bounds v.flex = 'WH' rootView.add_subview(v) rootView.present()
-
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.
-
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.
-
@bitbucket You might want to check the autoresizing behavior in the UI editor for the subviews of your 'test' view.
-
Are there any plans to make a copy/paste function for scripts utilizing the UI module, or am I just not aware of an existing similar function? Thanks in advance!
-
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?
-
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.
-
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.