【问题标题】:how to implement "Soft Keyboard Input Mode" and fix keyboard overlap issue in Xamarin.Forms iOS?如何在 Xamarin.Forms iOS 中实现“软键盘输入模式”并修复键盘重叠问题?
【发布时间】:2020-07-06 01:47:10
【问题描述】:

我在 Xamarin.Forms iOS 应用程序中实现“软键盘输入模式”和修复键盘重叠问题时遇到了麻烦。

我尝试过的以下解决方案:

我也试过以下文章

在没有“软键盘输入模式”的情况下还面临另一个问题,有时顶部和底部会出现空白,同时专注于在多次点击时触发键盘上下移动的输入控件。

我在两个页面中使用的以下页面布局设计(在两个页面中面临问题,我已经在使用滚动视图)

Page 1:

`<ContentPage.Content>
            <StackLayout >
                <ScrollView Margin="10,10,10,0">
                    <StackLayout x:Name="pageContentLayout">
                    </StackLayout>
                </ScrollView>
                <AbsoluteLayout HorizontalOptions="FillAndExpand" 
                                VerticalOptions="FillAndExpand">
                    <StackLayout x:Name="mainEntryLayout" 
                                 AbsoluteLayout.LayoutFlags="All" 
                                 AbsoluteLayout.LayoutBounds="0,0,1,1">
                        <ActivityIndicator x:Name="entryIndicator" 
                                   IsVisible="False" 
                                   IsRunning="False">
                        </ActivityIndicator>
                        <Grid VerticalOptions="EndAndExpand" 
                          HorizontalOptions="EndAndExpand"
                          x:Name="entryGrid" 
                          BackgroundColor ="{StaticResource Color}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="85*"/>
                                <ColumnDefinition Width="15*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="60"/>
                            </Grid.RowDefinitions>
                            <custom:CustomEntry CornerRadius="4"  
                                                IsCurvedCornersEnabled="True"  
                                                BorderColor="{StaticResource Color}"                     
                                                PlaceholderColor="{StaticResource Color}"                     
                                                TextColor="{StaticResource Color}"   
                                                VerticalOptions="FillAndExpand" 
                                                HeightRequest="100"
                                                HorizontalOptions="FillAndExpand" 
                                                Grid.Column="0" 
                                                FontSize="12"                   
                                                BackgroundColor ="{StaticResource Color}"
                                                x:Name="commentCustomEntry"
                                                Keyboard="Chat"                 
                                                Margin="10"/>
                            <ImageButton VerticalOptions="CenterAndExpand" 
                                         HorizontalOptions="CenterAndExpand"                                                     
                                         Grid.Column="1"
                                         HeightRequest="30" 
                                         WidthRequest="30" 
                                         x:Name="SendBtn"
                                         BackgroundColor="Transparent"/>
                        </Grid>
                    </StackLayout>
                    <StackLayout x:Name="pageIndicatorLayout" 
                                 IsVisible="false"                              
                                 AbsoluteLayout.LayoutFlags="PositionProportional"
                                 AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">
                        <Label x:Name="loadingLabel" TextColor="{StaticResource Color}"/>
                        <ActivityIndicator IsRunning="true" 
                                           Color ="{DynamicResource Color}"/>
                    </StackLayout>
                </AbsoluteLayout>
            </StackLayout>
        </ContentPage.Content>
    `


Page 2:



`
    <StackLayout>
            <AbsoluteLayout HorizontalOptions="FillAndExpand" 
                                VerticalOptions="FillAndExpand">
                <StackLayout x:Name="pageContentLayout" 
                                 AbsoluteLayout.LayoutFlags="All" 
                                 AbsoluteLayout.LayoutBounds="0,0,1,1">
                    <StackLayout x:Name="ImgLayout" ></StackLayout>
                    <ScrollView>
                        <StackLayout>
                            <StackLayout Orientation="Vertical"  
                                         HorizontalOptions="FillAndExpand">
                                <Label Text="{Binding titleLable}" 
                                       FontAttributes="Bold" 
                                       TextColor="{StaticResource Color}" 
                                       Margin="10, 0, 0, 0" 
                                       FontSize="14"/>
                                <Label x:Name="resourceLabel"
                                       TextColor="{StaticResource Color}" 
                                       FontSize="11" 
                                       Margin="10, 0, 0, 0" />
                                <Label Text="{Binding sourceLabel}" 
                                       TextColor="{StaticResource Color}" 
                                       FontSize="11" 
                                       FontAttributes="Bold" 
                                       Margin="10, 0, 0, 0" />
                                <Label x:Name="personLabel"
                                       TextColor="{StaticResource Color}" 
                                       FontSize="11" 
                                       Margin="10, 0, 0, 0" />
                                <Label FontAttributes="Bold, Italic"    
                                   FontSize="12" 
                                   Padding="10,10,10,0"
                                   Text="{Binding hightlightLabel}"/>
                            </StackLayout>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <local:ExtendedWebViewModel x:Name="webContentView"
                                                        Grid.Row="0"
                                                        HorizontalOptions="FillAndExpand"
                                                        VerticalOptions="FillAndExpand"/>
                            </Grid>
                        </StackLayout>
                    </ScrollView>
                    <Grid Padding="10,0,10,0" RowSpacing="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="60*"/>
                            <ColumnDefinition Width="40*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>                    
                        <StackLayout Grid.Column="1"
                                     Grid.Row="0"
                                     Grid.RowSpan="2"
                                     Orientation="Horizontal" 
                                     HorizontalOptions="EndAndExpand"
                                     VerticalOptions="CenterAndExpand">
                            <Image Source="{Binding lImg, Converter={StaticResource Converter}}"
                                                   HeightRequest="20" 
                                                   WidthRequest="20">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="OnLImgTapped" 
                                                                          CommandParameter="{Binding item}"/>
                                </Image.GestureRecognizers>
                            </Image>
                            <Label x:Name="tCount" 
                                                   VerticalOptions="CenterAndExpand"
                                                   Text="{Binding tCount}" 
                                                   FontSize="12">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="OntCountTapped" 
                                                                          CommandParameter="{Binding item}"/>
                                </Label.GestureRecognizers>
                            </Label>
                            <Image Source="{Binding cImg, Converter={StaticResource Converter}}"
                                                   HeightRequest="20" 
                                                   WidthRequest="20">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="OnCImgTapped" 
                                                                          CommandParameter="{Binding item}"/>
                                </Image.GestureRecognizers>
                            </Image>
                            <Label Text="{Binding cCount}"
                                                   VerticalOptions="CenterAndExpand"
                                                   FontSize="12">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="OnCCountTapped" 
                                                                          CommandParameter="{Binding item}"/>
                                </Label.GestureRecognizers>
                            </Label>
                            <Image Source="{Binding SImg, Converter={StaticResource Converter}}"
                                                   HeightRequest="20" 
                                                   WidthRequest="20">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer Tapped="OnSImgTapped" 
                                                                          CommandParameter="{Binding item}"/>
                                </Image.GestureRecognizers>
                            </Image>
                        </StackLayout>
                    </Grid>
                    <Grid VerticalOptions="EndAndExpand" 
                          HorizontalOptions="EndAndExpand"
                          x:Name="entryGrid" 
                          BackgroundColor ="{StaticResource Color}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="85*"/>
                            <ColumnDefinition Width="15*"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="60"/>
                        </Grid.RowDefinitions>
                        <custom:CustomEntry CornerRadius="4"  
                                                IsCurvedCornersEnabled="True"  
                                                BorderColor="{StaticResource Color}"                     
                                                PlaceholderColor="{StaticResource Color}"
                                                TextColor="{StaticResource CommentEntryValueColor}"   
                                                VerticalOptions="FillAndExpand" 
                                                HeightRequest="100"
                                                HorizontalOptions="FillAndExpand" 
                                                Grid.Column="0"                        
                                                FontSize="12"                   
                                                BackgroundColor ="Color"
                                                x:Name="commentCustomEntry"
                                                Keyboard="Chat"                 
                                                Margin="10"/>
                        <ImageButton VerticalOptions="CenterAndExpand" 
                                         HorizontalOptions="CenterAndExpand"Grid.Column="1"
                                         HeightRequest="30" 
                                         WidthRequest="30" 
                                     Clicked="OnSendButtonClicked"
                                         x:Name="SendBtn"
                                         BackgroundColor="Transparent"/>
                    </Grid>
                </StackLayout>
                <StackLayout x:Name="pageIndicatorLayout" 
                                 IsVisible="false" 
                                 Padding="12"
                                 AbsoluteLayout.LayoutFlags="PositionProportional"
                                 AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">
                    <Label x:Name="loadingLabel" TextColor="{StaticResource Color}"/>
                    <ActivityIndicator IsRunning="true" 
                                           Color ="{DynamicResource Color}"/>
                </StackLayout>
            </AbsoluteLayout>
        </StackLayout>
`

您能否提出更好的解决方案?

谢谢

【问题讨论】:

  • 您是否按照xamgirl.com/… 中的确切步骤操作?
  • @AndroDevil 是的,我遵循了确切的步骤,但在我的情况下,我使用的是堆栈布局。

标签: ios xamarin.forms custom-renderer


【解决方案1】:

如果您只针对 ios,请移除滚动视图并尝试此插件。 https://github.com/paulpatarinski/Xamarin.Forms.Plugins/tree/master/KeyboardOverlap

安装此 nuget 后, 在 Xamarin.Forms.Init(). 之后的 Appdelegate 类中添加这些行

KeyboardOverlapRenderer.Init ();

除了初始化插件之外,您无需执行任何其他操作。键盘将不再与您的控件重叠!!!

【讨论】:

  • 你是在真机上测试吗?
  • 是的,我正在真实设备中进行测试。
  • 您是否正在尝试开发任何聊天 UI?
猜你喜欢
  • 2021-08-27
  • 2017-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 1970-01-01
相关资源
最近更新 更多