Forum Archive

How to lock landscape orientation? Code view.present(orientations=("landscape",)) does not work anymore

idchlife

I still can rotate my iPad and have portrait orientation. How to disable it?

Thanks!

cvp

@idchlife see help of ui.View.present

The orientations parameter has no effect on iPads starting with iOS 10 
because it is technically not possible to lock the orientation in an app 
that supports split-screen multitasking.
idchlife

Thanks! Wow, that's a bummer. Makes sense, though

JonB

You can always use the rotation lock hardware switch (or in the bottom settings menu if your hardware button is set to be mute

JonB

actually, you could detect rotation in layout, then use transform.rotation to manually undo it.

ellie_ff1493

you might be able to get the gravity vector and flip it back whenever it flips

Drizzel

@JonB I have the same question, though a I have never used these functions before. Can you explain this in a coding statement? I assume it will have to run in the update(self) section?

JonB

https://forum.omz-software.com/topic/3115/ui-and-device-rotation-monitoring/2

Your root view would be one the custom views above, which implement layout. Then within layout, you can check the orientation using ui.WebView().eval_js('window.orientation') returns 0, 90, 180, or 270. (it's possible the webview needs to be added to a view and visible for this to work, though it can be 1x1)

You would then use self.transform=ui.Transform.rotation(angle), and also reset the size to proper w and height, which you will have to swap when it rotations 90 degrees.

AddledBadger

Hi,

I'd like to lock a view to portrait on the iPhone, and the code below doesn't work to do that, for me.

So just to clarify:

view.present(orientations = ['portrait'])

no longer works?

What's the preferred way to lock orientation now? Will the documentation get updated? Once the suggestion above has detected the orientation, how can you lock a Navigation Controller to the correct orientation?

Thanks!

import ui

global FRAME_WIDTH
global FRAME_HEIGHT
message = 'Blah'

def scrollview(message):
  sv = ui.ScrollView() 
  sv.background_color = 'blue'
  FRAME_WIDTH = ui.get_screen_size().width - 2
  FRAME_HEIGHT = ui.get_screen_size().height - 66 
  sv.frame = (1, 1, FRAME_WIDTH, FRAME_HEIGHT)     
  sv.content_size = FRAME_WIDTH, FRAME_HEIGHT + 500 
  view.add_subview(sv) 

  tv = ui.TextView()
  tv.background_color = 'red'
  tv.frame = (2, 2, FRAME_WIDTH - 4, FRAME_HEIGHT - 4 + 500) 
  tv.font = ('Palatino', 18)
  tv.editable = False
  tv.text = message
  sv.add_subview(tv)

view = ui.View()                                     
view.name = 'Demo'                                    
view.background_color = 'black'                       
view.frame=(0, 0, 400, 400)

scrollview(message)

view.present(orientations = ['portrait'])

mikael

@AddledBadger, orientation can still be locked on iPhone, but not with the ”new buggy default” 'sheet' presentation mode - you need to remember to specify 'fullscreen':

view.present('fullscreen', orientations=['portrait'])
AddledBadger

Superb, thanks, working now.