2
1

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.

Xamarin.Forms SafeAreaをTopだけ設けたい場合

Posted at

##Xamarin.FormsでTopのステータスバー部分だけセーフエリアを設けたい時があると思う。

結論としては

TestPage.xaml.cs

protected override void OnAppearing()
{
    base.OnAppearing();
    if( Device.RuntimePlatform == Device.iOS )
    {
        var safeInsets = On<Xamarin.Forms.PlatformConfiguration.iOS>().SafeAreaInsets();
        safeInsets.Bottom = 0;
        this.Padding = safeInsets;
    }
}

セーフエリアがある場合は、Bottomも値が入っているので0を代入してあげてマージンを設定してあげるだけ

実に簡単だがあまり情報がなかったので参考にしてほしい。
個人的にはXAML側で記述したいところなので良いコードがあれば情報共有したいと思います。

###おまけ
上下ともSafeAreaがあっても良い場合

TestPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:views="clr-namespace:StampApp.Views"
             xmlns:vm="clr-namespace:StampApp.ViewModel"
             x:Class="StampApp.Page.TestPage"
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core" 
             ios:Page.UseSafeArea="true">

XAML側で
ios:Page.UseSafeArea="true"にするだけ

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?