Setting orientation of one page

The app I'm currently working on requires me to set the orientation of one single page to landscape (the rest of the app is strictly portrait).

However I can't find a simple way to do so.

Has anyone found a way?

Tagged:

Posts

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @Info-FrameLtd :

    I have done it in iOS, I don't know well about other.
    These links will help you surely. :)
    SETTING ORIENTATION IN XAMARIN iOS
    Preventing Rotation on iOS on a Specific Page
    Handling Orientation :blush:

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @Info-FrameLtd I'm currently working on this, but I only have the solution for Android yet. I couldn't get it to work on iOS right now.

    You have to add the code to MainActivity.cs
    So this is how I do it in Android:

    First add those to [Activity(...., ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] or your app will throw an Exception if the user turn his device.

    Then
    //To check if device is allowed to rotate
    private bool _allowLandscape;

    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        private bool _allowLandscape;
    
        protected override void OnCreate(Bundle bundle)
        {
            switch (Device.Idiom)
            {
                case TargetIdiom.Phone:
                    RequestedOrientation = ScreenOrientation.Portrait;
                    break;
                case TargetIdiom.Tablet:
                    RequestedOrientation = ScreenOrientation.Landscape;
                    break;
            }
    
            base.OnCreate(bundle);
    
            //Setup additional stuff that you need
    
            //Calls from the view that should rotate
            MessagingCenter.Subscribe<GraphicsView>(this, "graphic", sender =>
            {
                _allowLandscape = true;
                OnConfigurationChanged(new Configuration());
            });
    
            //When the page is closed this is called
            MessagingCenter.Subscribe<GraphicsView>(this, "return", sender =>
            {
                _allowLandscape = false;
                OnConfigurationChanged(new Configuration());
            });
    
            LoadApplication(new App());
        }
    
    public override void OnConfigurationChanged(Configuration newConfig)
    {
        base.OnConfigurationChanged(newConfig);
    
        switch (newConfig.Orientation)
        {
            case Orientation.Landscape:
                switch (Device.Idiom)
                {
                    case TargetIdiom.Phone:
                        if (!_allowLandscape)
                        {
                            LockRotation(Orientation.Portrait);
                        }
                        break;
                    case TargetIdiom.Tablet:
                        LockRotation(Orientation.Landscape);
                        break;
                }
                break;
            case Orientation.Portrait:
                switch (Device.Idiom)
                {
                    case TargetIdiom.Phone:
                        if (!_allowLandscape)
                        {
                            LockRotation(Orientation.Portrait);
                        }
                        break;
                    case TargetIdiom.Tablet:
                        LockRotation(Orientation.Landscape);
                        break;
                }
                break;
            case Orientation.Undefined:
                if (Device.Idiom == TargetIdiom.Phone && _allowLandscape)
                {
                    LockRotation(Orientation.Landscape);
                }
                else if (Device.Idiom == TargetIdiom.Phone && !_allowLandscape)
                {
                    LockRotation(Orientation.Portrait);
                }
                break;
            }
        }
    
    private void LockRotation(Orientation orientation)
    {
        switch (orientation)
        {
            case Orientation.Portrait:
                RequestedOrientation = ScreenOrientation.Portrait;
                break;
            case Orientation.Landscape:
                RequestedOrientation = ScreenOrientation.Landscape;
                break;
        }
    }
    
  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @Info-FrameLtd Ok, I managed to get something to work on iOS but it still is not working 100%

    Take a look at this:

    In the AppDelegate.cs I add a bool value just like in the Android one

    private bool _allowRotation;
    

    Then in the FinishedLaunching method I add those 2 Listeners

    MessagingCenter.Subscribe<DataResultStack>(this, "graphic", sender =>
    {
        _allowRotation = true;
    });
    
    MessagingCenter.Subscribe<GraphicsView>(this, "return", sender =>
    {
        _allowRotation = false;
    });
    

    Important: I changed the MessagingCenter.Send time from the OnAppearing method in the GraphicView to the View that is shown before. So when the User want's to Navigate to the Graphic, the call is send before Navigation.PushAsync() gets called.

    This is the code that I added in AppDelegate.cs. Unfortunately the Device is NOT rotated! You have to rotate it yourself and I can't find a solution that the Device will automatically rotate.

    GetSupportedInterfaceOrientations is called every time a View appears.

    public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
    {
        switch (Device.Idiom)
        {
            case TargetIdiom.Phone:
                if (_allowRotation)
                {
                    return UIInterfaceOrientationMask.LandscapeRight;
                }
                else
                {
                    return UIInterfaceOrientationMask.Portrait;
                }
            case TargetIdiom.Tablet:
                return UIInterfaceOrientationMask.Landscape;
            default:
                return UIInterfaceOrientationMask.Portrait;
        }
    }
    

    So, if anyone has an idea, please share it!

  • ibrahimnadaibrahimnada USMember ✭✭

    Hello Guys i want to make My log in Page portrait Only
    the other pages is okay to Rotate How can i do so ? pls hellp

  • MarioGSMarioGS USMember ✭✭

    @YkshLeo said:
    @Info-FrameLtd :

    I have done it in iOS, I don't know well about other.
    These links will help you surely. :)
    SETTING ORIENTATION IN XAMARIN iOS
    Preventing Rotation on iOS on a Specific Page
    Handling Orientation :blush:

    Hi, @YkshLeo

    You have a complete example. some links no loger work.

  • SuchithSuchith INMember ✭✭

    @RaphaelSchindler is that approach is working for you?. Once after setting the RequestedOrientation , OnConfigurationChanged is not calling again.

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @Suchith Yes, this is still working for me. Could you show some code? Maybe somethings faulty there

  • SuchithSuchith INMember ✭✭

    @RaphaelSchindler thanks for the response. i solved that issue, it was bcz once u set app to one orientation again it wont check device orientation in any of the other screens again(OnConfigurationChanged vl not fire). So whenever i need to check orientation changed as follows,
    RequestedOrientation = ScreenOrientation.Unspecified;

  • RaphaelSchindlerRaphaelSchindler USMember ✭✭✭

    @Suchith Yeah it's a little bit weird in Forms how this is handled. Glad you could figure it out :)

Sign In or Register to comment.