ios - Xamarin.Forms 和 iOS : how to combine UseSafeArea and a background image?
<p><p>我有一个关于 iOS 使用 <strong>安全区域</strong> 的问题。</p>
<p>我通过<code>RelativeLayout</code> 使用<strong>背景图片</strong>,并在此背景图片上显示<strong>表单</strong>。我在表单的容器上为 iOS 使用了 <code>margin</code>:这很好用,但是 <strong>iPhone X</strong> 上的渲染效果不是很好。</p>
<pre><code><RelativeLayout>
<Image Source="background.jpg" Opacity="0.75"
Aspect="AspectFill"
RelativeLayout.WidthConstraint =
"{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint =
"{ConstraintExpression Type=RelativeToParent, Property=Height}" />
<ScrollView RelativeLayout.WidthConstraint =
"{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint =
"{ConstraintExpression Type=RelativeToParent, Property=Height}">
<ScrollView.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0, 20, 0, 0" />
</OnPlatform>
</ScrollView.Margin>
<StackLayout>
<!-- Header -->
<StackLayout VerticalOptions="Start">
<fnc:HunterHeader />
</StackLayout>
<!-- Form -->
<StackLayout VerticalOptions="CenterAndExpand"
Spacing="6" Margin="20">
<!-- ... -->
</StackLayout>
</StackLayout>
</RelativeLayout>
</code></pre>
<p>所以我尝试将 <code>UseSafeArea</code> 设置为 <strong>true</strong>,但我得到了顶部和底部边距。 </p>
<p><strong><em>是否有可能解决这个问题,并将 UseSafeArea 和背景图像结合起来?
或者有没有办法只为 iPhone X 添加特定的边距?</em></strong> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><h1>选项 1 - 将安全区域应用于特定控件而不是页面</h1>
<p>安全区域可以设置在特定控件而不是整个页面上。例如,可以将安全区域值设置为 <code>ScrollView</code> 的边距或填充。还需要考虑方向更改(如果您的应用支持它们)。</p>
<p><strong>XAML</strong></p>
<pre class="lang-xaml prettyprint-override"><code><RelativeLayout>
<Image Aspect="AspectFill" Source="background.png"
RelativeLayout.WidthConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Height}"/>
<ScrollView x:Name="scrollView"
RelativeLayout.WidthConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Width}"
RelativeLayout.HeightConstraint = "{ConstraintExpression Type=RelativeToParent, Property=Height}">
<StackLayout Margin="20, 0" Spacing="20">
<!-- Header -->
<StackLayout>
<BoxView BackgroundColor="Black" HeightRequest="50" />
</StackLayout>
<!-- Form -->
<StackLayout Spacing="20">
<BoxView BackgroundColor="White" HeightRequest="150" />
<BoxView BackgroundColor="White" HeightRequest="150" />
<BoxView BackgroundColor="White" HeightRequest="150" />
<BoxView BackgroundColor="White" HeightRequest="150" />
<BoxView BackgroundColor="White" HeightRequest="150" />
<BoxView BackgroundColor="White" HeightRequest="150" />
</StackLayout>
</StackLayout>
</ScrollView>
</RelativeLayout>
</code></pre>
<p><strong>代码隐藏</strong></p>
<pre class="lang-cs prettyprint-override"><code> private double width = 0;
private double height = 0;
private bool safeAreaSetInitially;
protected override void OnAppearing()
{
base.OnAppearing();
if (!this.safeAreaSetInitially)
{
this.SetSafeAreaOnContentContainer();
this.safeAreaSetInitially = true;
}
}
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (Math.Abs(this.width - width) > double.Epsilon || Math.Abs(this.height - height) > double.Epsilon)
{
this.width = width;
this.height = height;
if (this.width > this.height)
{
// reconfigure for landscape if needed
}
else
{
// reconfigure for portrait if needed
}
// reset safe area on rotation as the landscape/portrait safe areas are different
this.SetSafeAreaOnContentContainer();
}
}
private void SetSafeAreaOnContentContainer()
{
if (Device.RuntimePlatform == Device.iOS)
{
// set safe insets on content that you want to be within the safe area
var safeInsets = On<Xamarin.Forms.PlatformConfiguration.iOS>().SafeAreaInsets();
safeInsets.Top += 20; // add any other custom design margin specific to iOS (the extra 20 came from the original XAML)
this.scrollView.Margin = safeInsets;
}
}
</code></pre>
<p><strong>结果</strong></p>
<h2> <a href="/image/3onzl.gif" rel="noreferrer noopener nofollow"><img src="/image/3onzl.gif" alt="enter image description here"/></a> </h2>
<h1>选项 2 - 使用 <a href="https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.page.backgroundimage?view=xamarin-forms#Xamarin_Forms_Page_BackgroundImage" rel="noreferrer noopener nofollow">Page.BackgroundImage</a>属性</h1>
<p>根据要求,另一种选择是只使用 <code>Page</code> 的 <code>BackgroundImage</code> 属性(它不受 <code>On<Xamarin.Forms 的影响。 PlatformConfiguration.iOS>().SetUseSafeArea(true)</code> 方法)。这种方法的一个问题是 BackgroundImage 在 iOS 中是“平铺的”。要解决此问题,请创建一个不会“平铺”<a href="https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.page.backgroundimage?view=xamarin-forms#Xamarin_Forms_Page_BackgroundImage" rel="noreferrer noopener nofollow">BackgroundImage</a> 的自定义 <code>Xamarin.Forms.ContentPage</code> 渲染器。 .这是一个 <a href="https://forums.xamarin.com/discussion/89781/xamarin-forms-ios-prevent-tile-style-background-image-for-contentpage" rel="noreferrer noopener nofollow">example</a> .</p></p>
<p style="font-size: 20px;">关于ios - Xamarin.Forms 和 iOS : how to combine UseSafeArea and a background image?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/51553694/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/51553694/
</a>
</p>
页:
[1]