1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSのステータスバーの文字を白くする【Xamarin.Forms(Prism)】

Last updated at Posted at 2018-03-19

iOSのステータスバーの文字を白くする

Xamrin.FormsでPrism使ってるときにiOSアプリのステータスバーの文字が白くならなかったので試行錯誤した
一応できたけどこういうもんなの?ってモヤモヤが残る。
もっといい方法ありそう。

できたパターン①

.iOSプロジェクトのinfo.plistに

info.plist
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

起動時のスプラッシュでも変更する場合は以下も

info.plist
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>

PCLでNavigationPageを継承し、コンストラクタで設定する

CustomNavigationPage.cs
// usingとかnamespace省略
public class CustomNavigationPage : NavigationPage
{
    public CustomNavigationPage() : base()
    {
        // ナビゲーションバー、ステータスバーの背景色
        BarBackgroundColor = Color.Blue;
        // ナビゲーションバー、ステータスバーの文字色(ステータスバーは白か黒になる)
        BarTextColor = Color.White;
    }
}

App.xaml.csでコンテナに登録するNavigationPageをCustomNavigationPageに差し替える
nameはNavigationPageにしておくと差し替えの影響が少ないはず

App.xaml.cs
protected override async void OnInitialized()
{
    InitializeComponent();
    await NavigationService.NavigateAsync("NavigationPage/MainPage");
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    //containerRegistry.RegisterForNavigation<NavigationPage>();
    containerRegistry.RegisterForNavigation<CustomNavigationPage>("NavigationPage");
    containerRegistry.RegisterForNavigation<MainPage>();
}

ステータスバー文字色変更①.jpg

できたパターン②

info.plistは①と同様

App.xaml.csでNavigationAsyncしたあとにNavigationPageを取り出してプロパティ変更

App.xaml.cs
protected override async void OnInitialized()
{
    InitializeComponent();
    await NavigationService.NavigateAsync("NavigationPage/MainPage");

    var naviPage = (NavigationPage)MainPage;
    naviPage.BarBackgroundColor = Color.Green;
    naviPage.BarTextColor = Color.White;
}

Prismを使わないXamarin.FormsだとMainPageプロパティに最初のページを設定していた気がするので取り出してみたらいけた。
が、どう考えても①の方が安全

ステータスバー文字色変更②.jpg

ダメだったパターン

info.plistまでは同様

iOSプロジェクトのAppDelegate.csに追加

AppDelegate.cs
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();
    LoadApplication(new App(new iOSInitializer()));

    UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false); // ← これ

    return base.FinishedLaunching(app, options);
}

結構紹介されているパターンだがステータスバーの文字は黒いままだった。
これは最初からiOSのみのプロジェクトだろうか…?

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?