![](/img/trans.png)
[英]How do I change UIPickerView text color in renderer view using Xamarin.Forms.iOS?
[英]How do I make Xamarin.Forms.iOS View scroll to the Entry that is in focus?
我有一个登录页面,在一个大图像徽标下有 3 个 Entry 控件,在页面底部有一个 Button 控件,当我单击顶部的 Entry 控件以使 Entry 成为焦点以开始输入时,键盘出现并覆盖了下面的输入控件和登录按钮不是最好的用户体验。
The user can only scroll up manually on Xamarin.Forms.iOS but using the same code, ScrollToAsync, on Android I have been able to make the Entry on focus scroll to the top of page by grabbing the parent ScrollView and doing "ScrollView.ScrollToAsync (EntryControl, ScrollToPosition.Start, true);"
在 Android 上,页面底部的按钮也通过在中间空白区域的 Grid Row 定义中使用 * 向上移动。
It seems like Xamarin.Forms.iOS behaves completely different to Xamarin.Forms.Android when rendering. 我已经看到 ScrollToAsync 在页面加载时在 iOS 上有一些问题,因为 ScrollView 在页面完全加载之前不存在。 但是这个动作是在已经完全呈现的页面上进行的。 我究竟做错了什么?
我已经尝试过这个 SO 中提到的延迟,但它没有帮助。 ScrollToAsync 在 Xamarin.Forms 中不起作用
XAML
<Grid>
<ScrollView VerticalOptions="FillAndExpand"
BackgroundColor="White"
x:Name="ScrollViewContainer"
>
<Grid Margin="15, 0, 15, 10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" /><!--Logo-->
<RowDefinition Height="Auto" /><!--Entry1-->
<RowDefinition Height="Auto" /><!--Entry2-->
<RowDefinition Height="Auto" /><!--Entry3-->
<RowDefinition Height="*" />
<RowDefinition Height="Auto" /><!--Button-->
<RowDefinition Height="20" /><!--Empty padding-->
</Grid.RowDefinitions>
<Image Grid.Row="0"
Aspect="AspectFit"
Source="CompanyLogo"
HorizontalOptions="Start"
HeightRequest="300"
VerticalOptions="Start"
Margin="10, 20, 20, 0"
/>
<Entry Grid.Row="1"
x:Name="BranchName"
Placeholder="BranchName"
Focused="BranchName_Focused"
/>
<Entry Grid.Row="2"
x:Name="Username"
Placeholder="e.g. Batman "
Focused="Username_Focused"
/>
<Entry Grid.Row="3"
x:Name="Password"
Placeholder="Enter your password"
Focused="Password_Focused"
IsPassword="True"
/>
<Button Grid.Row="5"
x:Name="LoginButton"
Text="Log in"
/>
</Grid>
</ScrollView>
</Grid>
代码背后
private async void BranchName_Focused(object sender, FocusEventArgs e)
{
await ScrollViewContainer.ScrollToAsync(BranchName, ScrollToPosition.Start, true);
}
解决方案部分是由上面的@numan98 回答让我重新审视微软文档https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.scrollview.scrolltoasync?view = xamarin 形式
它指出 ScrollToAsync(double X, double Y, bool animate) 将是 X 和 Y 应该是的最终双 position 值。
因此,X 不会在任何地方移动,所以我认为它应该是 0,而 YI 只是给出了一个任意数字,例如 250。我相信接近顶部的某个地方。
之后,我们又遇到了 iOS 错误,您需要延迟。
最终的代码看起来像这样。
private async void BranchName_Focused(object sender, FocusEventArgs e)
{
Device.StartTimer(TimeSpan.FromSeconds(0.25), () =>
{
scroll.ScrollToAsync(0, 250, true);
return false;
);
}
我希望这可以帮助别人。 接下来的事情是让 CTA 按钮移动并始终显示在视野中。
将此添加到您的 xaml.cs 文件中
protected override void OnAppearing()
{
base.OnAppearing();
userName.Focused += InputFocused;
password.Focused += InputFocused;
userName.Unfocused += InputUnfocused;
password.Unfocused += InputUnfocused;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
userName.Focused -= InputFocused;
password.Focused -= InputFocused;
userName.Unfocused -= InputUnfocused;
password.Unfocused -= InputUnfocused;
}
void InputFocused(object sender, EventArgs args)
{
Content.LayoutTo(new Rectangle(0, -270, Content.Bounds.Width, Content.Bounds.Height));
}
void InputUnfocused(object sender, EventArgs args)
{
Content.LayoutTo(new Rectangle(0, 0, Content.Bounds.Width, Content.Bounds.Height));
}
我觉得你应该试试这个...
private async void BranchName_Focused(object sender, FocusEventArgs e)
{
await ScrollViewContainer.ScrollToAsync(BranchName.Bounds.X, BranchName.Bounds.Y, true);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.