A few minor changes to get HeaderClass working without a parent
# coding: utf-8
import ui
_demo_headers = ['one', 'two', 'three', 'four', 'five']
_header_heigth = 32
class MyCustomClass(ui.View):
def __init__(self, frame):
self.frame = frame
self.background_color = 'white'
self.flex = 'WH'
self.img = ui.ImageView()
self.img.image = ui.Image.named('iob:alert_circled_256')
self.add_subview(self.img)
self.header = HeaderClass()
self.add_subview(self.header)
def layout(self):
# do resizing in the main class first
self.img.frame = (0, _header_heigth, self.width, self.height - _header_heigth)
# then call a layout method on my inc. views
self.header.user_layout(self.width, _header_heigth)
class HeaderClass(ui.View):
def __init__(self):
self.background_color = 'white'
for col, header in enumerate(_demo_headers):
lb = ui.Label(name = str(col))
lb.text = header
lb.alignment = ui.ALIGN_CENTER
lb.border_width = .5
self.add_subview(lb)
# if you want layout called on this custom
# class as i understand it.
self.flex = 'WH'
def layout(self):
# if the class has no parent class calling
# the shots...
if not self.superview:
self.user_layout(self.width, _header_heigth)
def user_layout(self, w, h):
self.width = w
self.height = h
num_headers = len(self.subviews)
cell_width = self.width / num_headers
for i, sv in enumerate(self.subviews):
sv.frame = (i * cell_width , 0, cell_width, h)
if __name__ == '__main__':
#f = (0,0,540, 576)
#mcc = MyCustomClass(f)
#mcc.present('')
h = HeaderClass().present()