[英]Overriding Back button navigation in 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;
}
在此解決方案中,您可以設置導航欄的樣式,例如標題的文本顏色或后退按鈕的圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.