ksumarine
ksumarine

Reputation: 782

Xamarin.Forms MasterDetailPage - remove/hide status bar on Master

I have tried finding and answer to this, but I have had no luck.

I would like to re-create the functionality of the Slack app, in that I would like to show/hide the status bar (time, battery, network, etc) when the user opens the Master page from the Detail. So, when viewing the Detail page, you should see the status bar...when you slide open to reveal the Master page, I would like it to fade away.

I've tried creating a custom renderer for the Master (ContentPage) adding:

UIApplication.SharedApplication.SetStatusBarHidden(true, UIStatusBarAnimation.Fade);

to the ViewDidLoad method, but it is not working. I also tried adding this in the ViewWillAppear method, but it also did not work.

I would like this to happen on both iOS and Android, but I'm currently only focused on the iOS app.

Any help would be greatly appreciated!

Disclaimer: I'm very new to xamarin, so be gentle if this is common knowledge.

Upvotes: 1

Views: 1407

Answers (2)

Failcookie
Failcookie

Reputation: 33

I don't have the reputation to make a comment, but I wanted to add a code modification to ksumarine's answer.

You now need to use public override void ViewDidLayoutSubviews() for the code to function properly. Works as expected otherwise!

Upvotes: 2

ksumarine
ksumarine

Reputation: 782

Well I got it figured out for iOS!

1: add a line to your Info.plist file in the iOS package:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

2: Create a custom renderer for your MasterDetailPage...

using YourProject;
using YourProject.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;

[assembly:ExportRenderer(typeof(YourMasterDetailPageToCustomize), typeof(MasterDetailCustomRenderer))]

namespace YourProject.iOS {
    public class MasterDetailCustomRenderer:PhoneMasterDetailRenderer {
        public MasterDetailCustomRenderer() {}
        public override void ViewWillLayoutSubviews() {
            var item = Element as MasterDetailPage;
            if (item != null) {
                if (item.IsPresented) {
                    UIApplication.SharedApplication.SetStatusBarHidden(true, UIStatusBarAnimation.Fade);
                } else {
                    UIApplication.SharedApplication.SetStatusBarHidden(false, UIStatusBarAnimation.Fade);
                }
            }
            base.ViewWillLayoutSubviews();
        }
    }
}

That's really it. It will not work unless you add the line to your Info.plist though. The expected behavior is to fade out the status bar when you open the Master page and fade back the status bar when you show the Detail page.

Upvotes: 2

Related Questions