微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin 形式的 iOS 标题栏图标在横向打开时没有获得正确的中心位置,转向纵向

如何解决Xamarin 形式的 iOS 标题栏图标在横向打开时没有获得正确的中心位置,转向纵向

我有一个 iOS 特定的问题,但在 Android 上不会发生。

如果应用中的页面以横向模式打开

enter image description here

然后转向在标题栏的中心纵向显示徽标,而不是像这样移动到中心离开页面

enter image description here

如果它以纵向打开然后变成横向,则它不会移动到中心或离开屏幕,而是向左移动,停留在全宽的四分之一左右。

XML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="PhotoApp.MobileApp.Views.PhotogaleryPage">
<ContentPage.Resources>
    <ResourceDictionary>
        <Color x:Key="Accent">#96d1ff</Color>
    </ResourceDictionary>
</ContentPage.Resources>
<Shell.TitleView>
    <Image
           HorizontalOptions="Center"
           VerticalOptions="Center" 
        Source="logo.png"  />
</Shell.TitleView>

解决方法

我建议你使用这个:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="PhotoApp.MobileApp.Views.PhotoGaleryPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <Color x:Key="Accent">#96d1ff</Color>
        </ResourceDictionary>
    </ContentPage.Resources>
    <Shell.TitleView>
  <StackLayout HorizontalOptions="Center"
            Margin="{OnPlatform iOS='0,25,0',Android='0,20,Default=0}"
            Orientation="Horizontal">
        <Image       
            Source="logo.png"  />
   </StackLayout>
    </Shell.TitleView>
,

和上面的答案一样,你可以这样试试:

<ContentPage>
    <NavigationPage.TitleView>
        <StackLayout Orientation="Horizontal" VerticalOptions="Center" Spacing="10">
            <Image Source="logo.png">               
            </Image>           
        </StackLayout>
    </NavigationPage.TitleView>
    ...
</ContentPage>

只需要一个 StackLayout 来包装图片,而不是直接把它放在 Shell.TitleView 中。

看起来像ios中的旋转导致了这个问题,因为你直接利用了Shell.TitleView中的Image。你可以想象一下:它真的在中间,但屏幕无法显示整个标题栏,这导致了你看到的问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。