Forum Archive

Separate portrait and landscape views

Dog2puppy

Hi. I recently started using Pythonista and was wondering if it’s possible to have separate views for portrait and landscape and to switch between them dynamically on the fly.

cvp

@Dog2puppy Hello, welcome here.

As usual, I'm not sure I correctly understand. Is this what you want?

import ui

class MyView(ui.View):

    def __init__(self):
        self.background_color = 'white'

        vp = ui.View(name='portrait')
        vp.flex = 'WH'
        vp.border_width = 4
        vp.border_color = 'red'
        self.add_subview(vp)
        vp.add_subview(ui.Label(frame=(10,10,100,32),text = vp.name))

        vl = ui.View(name='landscape')
        vl.flex = 'WH'
        vl.frame = self.bounds
        vl.border_width = 4
        vl.border_color = 'blue'
        self.add_subview(vl)
        vl.add_subview(ui.Label(frame=(10,10,100,32),text = vl.name))

    def layout(self):
        self['portrait'].hidden  = self.width > self.height
        self['landscape'].hidden = self.width < self.height

vp = MyView()
vp.present()
Dog2puppy

@cvp That does what I needed. Thanks! Also happy Easter if you celebrate it.

cvp

@Dog2puppy Thanks, same for you