【发布时间】:2019-02-07 19:18:53
【问题描述】:
我正在 Xamarin.Form 中创建应用程序,我是 Xamarin 的新手。在 IOS 中,应用程序在顶部滚动时弹跳。有没有办法阻止滚动弹跳?
我尝试运行 CSS 文件但没有成功。
【问题讨论】:
标签: c# ios xaml xamarin.forms
我正在 Xamarin.Form 中创建应用程序,我是 Xamarin 的新手。在 IOS 中,应用程序在顶部滚动时弹跳。有没有办法阻止滚动弹跳?
我尝试运行 CSS 文件但没有成功。
【问题讨论】:
标签: c# ios xaml xamarin.forms
在 iOS 上有一个专门的属性。这是AlwaysBounceVertical。还有一些与此相关的属性。
虽然 Forms 不支持直接设置这些属性,因此您需要创建自定义渲染器或同等功能。看看这个:
using NoBounceiOS.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(ListView), typeof(NoBounceRenderer))]
namespace NoBounceiOS.iOS
{
public class NoBounceRenderer : ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
if (Control != null)
{
Control.AlwaysBounceVertical = false;
}
}
}
}
【讨论】:
原因:在 Xamarin.forms for iOS 中有一些 UITableView 的属性(表单中的列表视图)。
@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again
@property(nonatomic) BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
@property(nonatomic) BOOL alwaysBounceHorizontal; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally
所以,只将AlwaysBounceVertical设置为false是行不通的。你应该将bounces设置为false。
解决方案: 正如@Gerald Versluis 所说,您可以使用 CustomRenderer。
public class MyListViewRenderer:ListViewRenderer
{
public MyListViewRenderer()
{
}
protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
Control.Bounces = false;
}
}
}
【讨论】: