首页 > 解决方案 > 使用 xamarin 表单从页面中删除向后滑动手势

问题描述

有没有办法可以在我的项目的一个页面上禁用 iOS 的向后滑动到上一页选项?

标签: xamarinxamarin.forms

解决方案


您可以通过实现自定义渲染器并为此设置正确的属性来实现此目的。您可以在下面看到一个示例实现。在这种情况下,正确的属性是InteractivePopGestureRecognizer您需要设置为 false 的。

这样做ViewWillAppear是为了NavigationController初始化。

using DisableSwipe.iOS;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ContentPage), typeof(NoBackSwipeRenderer))]
namespace DisableSwipe.iOS
{
    public class NoBackSwipeRenderer : PageRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            if (ViewController?.NavigationController != null)
                ViewController.NavigationController.InteractivePopGestureRecognizer.Enabled = false;
        }
    }
}

推荐阅读