Add label to scroll view in UI editor.
In this simple demonstration code, I am adding a label to a scroll view. The scroll view is placed using the UI editor and I am adding a label to it as a subview programmatically. The label does not show up. Any suggestions?
w,h = ui.get_screen_size() class Tally(object): def __init__(self): pass def add_tally(self,sender=None): self.ty = ui.Label() self.ty.frame = (0, 0, w, h*0.25) self.ty.bg_color = 'yellow' self.sv = ui.load_view()['scrollview1'] self.sv.add_subview(self.ty) t = Tally() t.add_tally() tc = ui.load_view('Tally Counter') tc.present(hide_title_bar=True)
I'm confused. you are presenting
t.svhas never been added to any view that you are presenting..... in order for a view to show up, it must be a subview of a presented view.
I tried to change your code as little as possible. But still would not write it like this. So, the coding is not correct, only in line with what you asked about.
import ui w,h = ui.get_screen_size() class Tally(ui.View): def __init__(self): pass def add_tally(self): self.ty = ui.Label() self.ty.frame = (0, 0, w, h*0.25) self.ty.bg_color = 'yellow' #self.sv = ui.load_view()['scrollview1'] self.sv = ui.ScrollView(frame = (0,0,w,h)) self.sv.add_subview(self.ty) self.add_subview(self.sv) t = Tally() t.add_tally() t.present() #tc = ui.load_view('Tally Counter') #tc.present(hide_title_bar=True)
I guess I get confused when adding objects to UI editor objects. Your example does it all programmatically which is fine.
@donnieh, I also had a lot of problems at first also. I was relying on the ui designer thinking it was easier. But a short time doing it programmatically, you won't see the point of doing it in the ui designer. Well that's my opinion anyway. The ui designer is great to get going with and look at the properties etc. I just found its actually a lot more work now. But again, it really help me in the start to get my head around things. I still have a long way to go :)
@donnieh. By getting involved here on the forum and trying to help as I have been helped so many times, I learnt something very cool from you today. Maybe a lot of people just know it, but I didn't. It was your method (self, sender = None). I thought that was really crazy. But today while I was coding, I had a small problem about reusing an already defined method in my custom class from a callback function. The defined method didn't take additional args other than self. But if I call that method from a callback it will fail, silently. For the callback to work it requires (self, sender). I guess I could have just passed None to sender when I called it as a direct method (I hadn't thought of that also), but that would be messy and confusing. Best option was to definfe as (self, sender =None). Nice and clean. Yes, I am sure for a lot of people easy. But I was happy to get it.
import ui class test(ui.View): def __init__(self): # create a simple btn in the view # sets it callback function self.btn = ui.Button(title = 'Ok') self.btn.action = self.do_something_stupid self.add_subview(self.btn) self.style() def style(self): # the view style self.background_color = 'white' # the button style btn = self.btn btn.border_width = 1 btn.background_color = 'red' btn.tint_color = 'white' btn.font = ('<system>', 10) def layout(self): btn = self.btn btn.x = btn.y = 100 btn.width = 200 btn.height = 128 ''' i didnt understand why you were making sender = None on your previous example. but today when i was coding i wanted to call a function in my class from a callback. i was about to write a wrapper when i remembered your syntax. so nice, i would not have thought of it otherwise. if only one function you could say so what, but as the projects get larger is so important. ''' def do_something_stupid(self, sender= None ): ''' the stupid action is to increase the size of the font by 5, each time the button is pressed or is called as a method but because sender = None, a callback can use it or can be called as a normal method of the class. of course, not smart to reference sender in this case(unless you test for a sender). just good for calling code with logic that does not care about the sender ''' self.btn.font = (self.btn.font, self.btn.font + 5) if __name__ == '__main__': x = test() x.present('sheet') for i in range(10): x.do_something_stupid() print x
Yes, every time I start a project in the UI editor I end up re-doing the entire UI in my code eventually. I think I just like the editor because it is cool.
As for the sender=None, I probably use it more than I should. Ha
This is an old thread but the functionality of how to make fields as children nodes of a scroll view is documented here: http://omz-software.com/editorial/docs/ios_workflows/special/action-custom-ui.html
Scroll View – A scrollable canvas, to add content to it, tap it and select “Subviews…” from the context menu.