アイキャッチ画像

.Net MAUIで画面の自動回転の制御方法

当記事では、.Net MAUIで画面の自動回転を制御する方法を紹介していきます。

紹介環境

当記事は以下の環境で作成しています。

開発環境

  • Visual Studio 2026
  • .Net 11
  • Android 16.0
  • iOS 27.2

対応方法

ファイル構造は以下で作成しています。良ければ参考にしてください。

ファイル構造

手順1:画面の自動回転を制御する列挙型を作成します

画面の自動回転を制御するために、以下の列挙型を作成します。

// 名前空間はプロジェクトに合わせてください。
namespace SampleApp.CustomControls;

public enum CustomScreenOrientation
{
    /// <summary>
    /// 未指定
    /// </summary>
    None,

    /// <summary>
    /// 縦向き(上のみ)に固定
    /// </summary>
    Portrait,

    /// <summary>
    /// 横向きに固定
    /// </summary>
    Landscape,

    /// <summary>
    /// 下以外のすべての方向
    /// </summary>
    All,

    /// <summary>
    /// すべての方向
    /// </summary>
    FullAll,
}

手順2:カスタムコントロールを作成します

画面の自動回転を制御するためのカスタムコントロールを作成します。カスタムコントロールでは、ScreenOrientationの添付プロパティを用意します。

また、プロパティが変更された際にAndroid、iOSともに画面の自動回転を更新するようにします。Androidは、RequestedOrientationの値を変更することで更新されます。iOSは、SetNeedsUpdateOfSupportedInterfaceOrientationsを呼ぶことで更新されます。

// 名前空間はプロジェクトに合わせてください。
namespace SampleApp.CustomControls;

public static class CustomPage
{
    public static CustomScreenOrientation GetScreenOrientation(BindableObject obj)
    {
        return (CustomScreenOrientation)obj.GetValue(ScreenOrientationProperty);
    }

    public static void SetScreenOrientation(BindableObject obj, CustomScreenOrientation value)
    {
        obj.SetValue(ScreenOrientationProperty, value);
    }

    public static readonly BindableProperty ScreenOrientationProperty =
        BindableProperty.CreateAttached("ScreenOrientation",
            typeof(CustomScreenOrientation),
            typeof(CustomPage),
            CustomScreenOrientation.None,
            BindingMode.OneTime,
            propertyChanged: OnScreenOrientationChanged);

    private static void OnScreenOrientationChanged(BindableObject bindable, object oldValue, object newValue)
    {
#if ANDROID
        CustomScreenOrientation? screenOrientation = newValue as CustomScreenOrientation?;
        if (!screenOrientation.HasValue) return;
        switch (screenOrientation.Value)
        {
            case CustomScreenOrientation.Portrait:
                Platform.CurrentActivity?.RequestedOrientation = Android.Content.PM.ScreenOrientation.Portrait;
                break;

            case CustomScreenOrientation.Landscape:
                Platform.CurrentActivity?.RequestedOrientation = Android.Content.PM.ScreenOrientation.Landscape;
                break;

            case CustomScreenOrientation.All:
                Platform.CurrentActivity?.RequestedOrientation = Android.Content.PM.ScreenOrientation.Sensor;
                break;

            case CustomScreenOrientation.FullAll:
                Platform.CurrentActivity?.RequestedOrientation = Android.Content.PM.ScreenOrientation.FullSensor;
                break;

            default:
                break;
        }
#elif IOS
        UpdateIOSScreenOrientation();
#endif
    }

#if IOS
    /// <summary>
    /// 【iOS用】ScreenOrientationを更新します。
    /// </summary>
    public static void UpdateIOSScreenOrientation()
    {
        if (Application.Current?.Windows.FirstOrDefault()?.Handler?.PlatformView is UIKit.UIWindow window)
        {
            // サポートされているインターフェイスの向きを更新します。
            window.RootViewController?.SetNeedsUpdateOfSupportedInterfaceOrientations();
        }
    }
#endif
}

手順3:【iOS】画面の自動回転を指定します

iOSは以下のようにAppDelegateで画面の自動回転を指定します。

using Foundation;
using SampleApp.CustomControls;
using UIKit;

// 名前空間はプロジェクトに合わせてください。
namespace SampleApp;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    [Export("application:supportedInterfaceOrientationsForWindow:")]
    public UIInterfaceOrientationMask SupportedInterfaceOrientationsForWindow(UIApplication application, UIWindow? window)
    {
        if (Shell.Current?.CurrentPage is null)
        {
            return UIInterfaceOrientationMask.All;
        }

        CustomScreenOrientation screenOrientation = CustomPage.GetScreenOrientation(Shell.Current.CurrentPage);
        switch (screenOrientation)
        {
            case CustomScreenOrientation.Portrait:
                return UIInterfaceOrientationMask.Portrait;

            case CustomScreenOrientation.Landscape:
                return UIInterfaceOrientationMask.Landscape;

            default:
                break;
        }

        return UIInterfaceOrientationMask.All;
    }
}

画面遷移時にSetNeedsUpdateOfSupportedInterfaceOrientationsを呼ぶことで、Pageで指定した画面の自動回転を反映させることができます。そのため、手順2で用意したCustomPage.UpdateIOSScreenOrientationを呼びます。

// 名前空間はプロジェクトに合わせてください。
namespace SampleApp;

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
    }

    protected override void OnNavigated(ShellNavigatedEventArgs args)
    {
#if IOS
        CustomControls.CustomPage.UpdateIOSScreenOrientation();
#endif
    }
}

利用方法

利用する際は、以下のようになります。例ではAllを指定しています。

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:control="clr-namespace:SampleApp.CustomControls"
    x:Class="SampleApp.Pages.SampleAll"
    control:CustomPage.ScreenOrientation="All"
    Title="SampleAll">
    <VerticalStackLayout>
        <Label
            Text="サンプル"
            VerticalOptions="Center"
            HorizontalOptions="Center" />
    </VerticalStackLayout>
</ContentPage>

動作イメージ

縦のイメージ

縦の動作イメージ

横のイメージ

横の動作イメージ

終わりに

画面の自動回転を制御するために、今回の記事を作成してみました。もともと、.Net 10で対応していましたが、.Net 11で動作確認した際にiOSが動作しなかったため、作成しなおしてみました。この記事が役に立てば幸いです。