wcoder / Xamarin.Plugin.DeviceOrientation

Cross-platform plugin to work with screen orientation of mobile device.
MIT License
61 stars 19 forks source link

Is there any way to prevent the orientation change? #13

Open kxgonzalez opened 6 years ago

kxgonzalez commented 6 years ago

Hi, I want to prevent the app to be rotated to portrait mode in certain scenarios, but I need it available so I can't disable it completely. I know we have the OrientationChanged event but it fires after the orientation changes.

In other words; I want the app to be available in landscape and landscape flipped mode. How can I achieve this?

I'm currently using Xamarin.Forms with PCL.

rakeshostwal commented 6 years ago

Hi @kxgonzalez , were you able to fix this?

wcoder commented 6 years ago

Hi @kxgonzalez

Try to use LockOrientation method, for example:

CrossDeviceOrientation.Current.LockOrientation(DeviceOrientations.Landscape);
rakeshostwal commented 6 years ago

@wcoder - DeviceOrientations.Landscape keeps device window on horizontal left orientation only, but I want app to rotate horizontally to left as well as right. Is it possible with this plugin?

wcoder commented 6 years ago

Currently, there is no such functionality.

But you can setup platform manifests "supported device orientation" https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/device-orientation?tabs=vswin for allow only landscape orientation

rakeshostwal commented 6 years ago

Yes, but that would set it on a complete application, right? I want to set only horizontal orientation on a specific screen.

kxgonzalez commented 6 years ago

I'm in the same situation. I have a multipurpose app; one mode has views designed to be vertical. I have no problem with this. The other mode is designed to be horizontal, so I want to be able to rotate it landscape and landscape inverted. I tried with some logic but it fires after the rotation and animation happens.

wcoder commented 6 years ago

Currently, this plugin supported lock of device orientation only for one direction.

This situation (more than one direction) will good enhancement for next version of the plugin.

I can propose the workaround for yours:

Manual override these methods on each platform:

Android

MainActivity.cs

public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) {}

iOS

AppDelegate.cs

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow){}

In these methods, you can lock the device orientation for the required page.

kxgonzalez commented 6 years ago

Nice, thank you!