Not a bad idea. Because of the way ui.View used to work (fixed set of attributes), it would require quite a few changes to support this directly, but it's fairly easy to write a factory function that instantiates views with arbitrary attributes, and (optionally) adds them to an existing parent view:def make_view(cls=ui.View, **kwargs): if not issubclass(cls, ui.View): raise TypeError('cls must be a subclass of ui.View') v = cls() for name in sorted(kwargs.keys()): value = kwargs[name] if name in ('parent', 'superview') and isinstance(value, ui.View): value.add_subview(v) else: name = name.strip('_') setattr(v, name, value) return v
Usage example:main_view = ui.View(frame=(0, 0, 400, 400)) make_view(ui.SegmentedControl, parent=main_view, frame=(10, 10, 380, 32), tint_color='green', _segments=['foo', 'bar', 'baz'], selected_index=1) main_view.present('sheet')
Now, you may be wondering why I prefixed the segments attribute with an underscore, and then stripped that underscore in make_view. This is basically just a trick to force that the attributes are set in a specific order, specifically, that the segments attribute is set before the selected_index because setting the selected index doesn't work if there are no segments yet. There aren't a lot of cases where the order matters, but I happened to come across one of them when I wrote this example, and this was the easiest workaround I could think of.