Moving custom rendered page below the status bar on iOS

时间:2016-10-15 17:24:34

标签: c# ios xamarin xamarin.forms nsviewcontroller

To position my application's pages below the iOS status bar, I have created a custom renderer for my pages. It is supposed to move a page below the bar, just like the following XAML would do:

<local:CustomRenderedPage.Padding>
    <OnPlatform x:TypeArguments="Thickness">
        <OnPlatform.iOS>
            0, 40, 0, 0
        </OnPlatform.iOS>
    </OnPlatform>
</local:CustomRenderedPage.Padding>

Therefore I have used this answer which I have converted to C#.

However, without any error, the page still appears behind the status bar. To check if the custom renderer is working I am changing the background color as well, which is working.

//manually adjust the frame of the main view to prevent it from appearing under the status bar.
var app = UIApplication.SharedApplication;

if (!app.StatusBarHidden)
{
    this.View.BackgroundColor = UIKit.UIColor.FromRGB(20, 100, 20); //works
    this.View.Frame = new CoreGraphics.CGRect(0.0, app.StatusBarFrame.Size.Height, this.View.Bounds.Size.Width, this.View.Bounds.Size.Height - app.StatusBarFrame.Size.Height); //  does not work
}

Log.WriteLine("Done");  // works

Am I doing something wrong, or why is this not working?

2 个答案:

答案 0 :(得分:0)

正如我之前所问的那样,你从哪里调用这段代码,你回答“来自我的自定义页面渲染器”。但主要的一点是你称之为渲染器的WHERE。 这是有效的代码。

  [assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
    namespace ButtonRendererDemo.iOS
    {
        class MainPageRenderer : PageRenderer
        {

            public override void ViewWillAppear(bool animated)
            {
                View.Frame = new CoreGraphics.CGRect(View.Frame.X, View.Frame.Y + 100, View.Frame.Width, View.Frame.Height);
            }
        }
    }

答案 1 :(得分:-1)

你可以在xaml

中完成
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:cv="clr-namespace:Xamarin.Forms;assembly=Xamarin.Forms.CarouselView"
            xmlns:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
            x:Class="ButtonRendererDemo.PageWithCarousel"
             Padding="0,100,0,0">

enter image description here