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



我正在尝试使用Xamarin.IOS框架覆盖IOS的默认后退按钮行为。我的ViewController中有一堆对象,我的要求是,当用户按下返回键导航到上一个ViewController时,我将仅在堆栈为空时进行导航,否则我将留在该屏幕(ViewController(中。为此,我尝试了几件事,比如覆盖ViewWillDisappear并将PopViewController设置为false,但我无法做到这一点。请引导。

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);
}

您可以在特定的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;
}

在这个解决方案中,您可以设置导航栏的样式,如标题的文本颜色或后退按钮的图标。

最新更新