I am currently updating some views for the iPhone X. According to this blog post it should be fairly streight-forward, but - like always - it's not as smooth as it's supposed to be.
If you are using the recommended way
public MyView()
{
InitializeComponent();
On<Xamarin.Forms.PlatformConfiguration.iOS>().SetUseSafeArea(true);
}
the items of the View are given a Margin
according to the safe area insets, but this applies to views like SearchBar
, too, which already adapt to the insets out of the box. Using the code above will result in the gray background of the search bar not taking the whole width, which looks quite strange.
Fortunately the On<iOS>
has an extension method SafeAreaInsets()
which returns the correct insets - under certain conditions - which one for example can assign to a BindableProperty
. Unfortunately the conditions under which we can obtain the insets are not very consistent.
- When
OnSizeAllocated
is called the first time,SafeAreaInsets()
will return0,0,0,0
- When the view is shown
SafeAreaInsets()
returns the correct value (inOnAppearing
) OnSizeAllocated
is called multiply after rotation- The first time
SafeAreaInsets()
returns the correct value - The second time
SafeAreaInsets()
returns0,0,0,0
- The first time
My current working solution is, to override both and only set my bindable property (which is bound to my views) if the Thickness
returned by SafeAreaInsets()
is not default(Thickness)
.
I'd like to know if there is a standard way to always get the correct insets, without having to check if the insets have a sound value.