【问题标题】:Xamarin.Forms MasterDetailPage - remove/hide status bar on MasterXamarin.Forms MasterDetailPage - 删除/隐藏 Master 上的状态栏
【发布时间】:2018-03-13 10:22:10
【问题描述】:

我已经尝试找到并回答这个问题,但我没有运气。

我想重新创建 Slack 应用程序的功能,因为我想在用户从详细信息打开主页面时显示/隐藏状态栏(时间、电池、网络等)。因此,在查看详细信息页面时,您应该会看到状态栏...当您滑动打开以显示母版页时,我希望它消失。

我尝试为 Master (ContentPage) 添加自定义渲染器:

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

到 ViewDidLoad 方法,但它不起作用。我也尝试在 ViewWillAppear 方法中添加它,但它也不起作用。

我希望这在 iOS 和 Android 上都发生,但我目前只专注于 iOS 应用。

任何帮助将不胜感激!

免责声明:我对 xamarin 很陌生,所以如果这是常识,请保持温和。

【问题讨论】:

    标签: xamarin xamarin.ios xamarin.android xamarin.forms


    【解决方案1】:

    好吧,我已经为 iOS 弄明白了!

    1:在 iOS 包中的 Info.plist 文件中添加一行:

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

    2:为您的 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();
            }
        }
    }
    

    原来如此。除非您将该行添加到您的 Info.plist 中,否则它将不起作用。预期的行为是在您打开 Master 页面时淡出状态栏,并在您显示 Detail 页面时淡出状态栏。

    【讨论】:

      【解决方案2】:

      我没有发表评论的声誉,但我想在 ksumarine 的答案中添加代码修改。

      您现在需要使用public override void ViewDidLayoutSubviews() 才能使代码正常运行。否则按预期工作!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        相关资源
        最近更新 更多