Skip to content

Cross-platform plugin to work with screen orientation of mobile device.

License

Notifications You must be signed in to change notification settings

wcoder/Xamarin.Plugin.DeviceOrientation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Device Orientation Plugin for Xamarin and Windows

Simple cross-platform plugin to work with screen orientation of mobile device.

iOS demo

Xamarin.Forms sample

Setup

  • Available on NuGet: NuGet Badge
  • Install into your PCL/NetStandard project and Platform Specific projects

Platform Support

Platform Version
Xamarin.iOS iOS 10+
Xamarin.Android API 19+
Windows 10 UWP 10.0.16299+

v2.0 updated to NetStandard 2.0

Deprecated platforms

  • Windows Phone Silverlight
  • Windows Phone RT
  • Windows Store RT

Implementations for unsupported platforms contains here.

API Usage

Call CrossDeviceOrientation.Current from any project or PCL to gain access to APIs.

/// <summary>
/// Gets current device orientation
/// </summary>
DeviceOrientations CurrentOrientation { get; }

When device orientation is changed you can register for an event to fire:

/// <summary>
/// Event handler when orientation changes
/// </summary>
event OrientationChangedEventHandler OrientationChanged;

You will get a OrientationChangedEventArgs with the orientation type:

public class OrientationChangedEventArgs : EventArgs
{
	public DeviceOrientations Orientation { get; set; }
}

public delegate void OrientationChangedEventHandler(object sender, OrientationChangedEventArgs e);

The DeviceOrientations enumeration has these members.

Member Value Description
Undefined 0 No display orientation is specified.
Landscape 1 Specifies that the monitor is oriented in landscape mode where the width of the display viewing area is greater than the height.
Portrait 2 Specifies that the monitor rotated 90 degrees in the clockwise direction to orient the display in portrait mode where the height of the display viewing area is greater than the width.
LandscapeFlipped 4 Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 180 degrees) to orient the display in landscape mode where the width of the display viewing area is greater than the height. This landscape mode is flipped 180 degrees from the Landscape mode.
PortraitFlipped 8 Specifies that the monitor rotated another 90 degrees in the clockwise direction (to equal 270 degrees) to orient the display in portrait mode where the height of the display viewing area is greater than the width. This portrait mode is flipped 180 degrees from the Portrait mode.

Call LockOrientation for set orientation and lock with disabling device auto-rotation:

/// <summary>
///     Lock orientation in the specified position
/// </summary>
/// <param name="orientation">Position for lock.</param>
void LockOrientation(DeviceOrientations orientation);

For disable the lock, call UnlockOrientation method:

/// <summary>
///     Unlock orientation
/// </summary>
void UnlockOrientation();

iOS Specific Support

Add this code for your ViewController where you want locking orientation:

public override bool ShouldAutorotate()
{
	// set plugin for handle of this method
	return DeviceOrientationImplementation.ShouldAutorotate;
}

public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations()
{
	// allow all orientations
	return UIInterfaceOrientationMask.AllButUpsideDown;
}

In your Info.plist need set all device orientations.

Xamarin.Forms iOS

If you want to lock orientation for the entire iOS application.

Add this code in your AppDelegate.cs:

[Export("application:supportedInterfaceOrientationsForWindow:")]
public UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, IntPtr forWindow)
{
    return DeviceOrientationImplementation.SupportedInterfaceOrientations;
}

Note: In this case, to lock/unlock orientation on the one screen, you must use the LockOrientation/UnlockOrientation methods.

Xamarin.Forms Android

In your MainActivity.cs, add overriding for changing orientation as here:

public override void OnConfigurationChanged(Configuration newConfig)
{
    base.OnConfigurationChanged(newConfig);

    DeviceOrientationImplementation.NotifyOrientationChange(newConfig.Orientation);
}

Note: This solution has only two state Portrait and Landscape.

Additional information

Troubleshooting

Contributors


© 2016-2019 MIT License