简体   繁体   English

覆盖 Xamarin.IOS 中的后退按钮导航

[英]Overriding Back button navigation in Xamarin.IOS

I am trying to override the default back button behaviour of IOS, using Xamarin.IOS framework.我正在尝试使用 Xamarin.IOS 框架覆盖 IOS 的默认后退按钮行为。 I have a stack of objects inside my ViewController, my requirement is that when the user presses back to navigate to previous ViewController, i will navigate only when my stack is empty otherwise i will stay in that screen(ViewController).我的 ViewController 中有一堆对象,我的要求是当用户按回导航到前一个 ViewController 时,我只会在我的堆栈为空时导航,否则我将留在该屏幕(ViewController)中。 For this i tried several thing, like overriding the ViewWillDisappear and setting the PopViewController to false but i am unable to do this.为此,我尝试了几件事,例如覆盖 ViewWillDisappear 并将 PopViewController 设置为 false,但我无法做到这一点。 Please guide.请指导。

public override void ViewWillDisappear(bool animated)
    {
        Stack<Object> st = vPage.uContentStack;
        if (st.Count != 0)
        {
            Object pop_obj = st.Pop();
            if (st.Count != 0)
            {
                NavigationController.PopViewController(false);// Here trying to stop navigating back
                Object peek_obj = st.Peek();
                vPage.ContentUpdateOnBackPress(pop_obj, peek_obj);
            }
            else
            {
                NavigationController.PopViewController(true);
            }

        }
        else
        {
            NavigationController.PopViewController(true);
        }
        base.ViewWillDisappear(animated);
    }

You could customize the Navigation Bar in the specific ViewController您可以在特定的ViewController 中自定义导航栏

public override void ViewWillAppear(bool animated)
    {
        base.ViewWillAppear(animated);
       
        NavigationController.NavigationBar.Hidden = true;
        double height = IsiphoneX();
        UIView backView = new UIView()
        {
            BackgroundColor = UIColor.White,
            Frame = new CGRect(0, 20, UIScreen.MainScreen.Bounds.Width, height),
        };
        UIButton backBtn = new UIButton()
        {
            Frame = new CGRect(20, height - 44, 40, 44),
            Font = UIFont.SystemFontOfSize(18),
        };
        backBtn.SetTitle("<", UIControlState.Normal);
       // backBtn.SetBackgroundImage(UIImage.FromBundle("xx.png"),UIControlState.Normal); or you can set image here 
        backBtn.SetTitleColor(UIColor.FromRGB(60,140,250), UIControlState.Normal);
        backBtn.AddTarget(this, new Selector("GoBack"), UIControlEvent.TouchUpInside);
        UILabel titleLabel = new UILabel()
        {
            Frame = new CGRect(UIScreen.MainScreen.Bounds.Width / 2 - 75, 0, 150, height),
            Font = UIFont.SystemFontOfSize(20),
            Text = "xxx",
            TextAlignment = UITextAlignment.Center,
            TextColor = UIColor.Black,
            Lines = 0,
        };
        UILabel line = new UILabel()
        {
            Frame = new CGRect(0, height, UIScreen.MainScreen.Bounds.Width, 0.5),
            BackgroundColor = UIColor.Black,
        };

        backView.AddSubview(backBtn);
        backView.AddSubview(titleLabel);
        backView.AddSubview(line);
        View.AddSubview(backView);
    }
    double IsiphoneX()
    {
        double height = 44;
        if (UIDevice.CurrentDevice.CheckSystemVersion(11, 0))
        {
            if (UIApplication.SharedApplication.Delegate.GetWindow().SafeAreaInsets.Bottom > 0.0)
            {
                height = 64;
            }
        }
        return height;
    }
    [Export("GoBack")]
    void GoBack()
    {
       //handle logic here
    }
    public override void ViewWillDisappear(bool animated)
    {
        base.ViewWillDisappear(animated);

        NavigationController.NavigationBar.Hidden = false;
    }

In this solution you could set the style of the navigation bar , like text color of title or icon of back button .在此解决方案中,您可以设置导航栏的样式,例如标题的文本颜色或后退按钮的图标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM