我尝试使用此component和此example在Xamarin.iOS中实现侧边菜单。 除了要关闭侧边菜单的部分外,一切正常。
现在它的工作原理如下:我可以通过使用NavigationItem中的LeftButton或手指滑动来打开侧边菜单。但我不能用这两种方法中的任何一种来关闭它。
有谁知道为什么会这样?我错过了什么?
另见下面的代码。
HomeView
public partial class HomeView : MvxViewController
{
public HomeView() : base("HomeView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
CGRect screenRect = UIScreen.MainScreen.Bounds;
NavigationController.View.Frame = new CGRect(0, 0, screenRect.Width, screenRect.Height);
var app = UIApplication.SharedApplication.Delegate as AppDelegate;
NavigationItem.SetLeftBarButtonItem(
new UIBarButtonItem(UIImage.FromBundle("menu"),
UIBarButtonItemStyle.Plain, (sender, e) =>
{
app.SidebarController.ToggleMenu();
}), true);
}
RootView
public partial class RootView : MvxViewController
{
public RootViewModel RootViewModel
{
get { return (RootViewModel)ViewModel; }
set { ViewModel = value; }
}
public RootView() : base("RootView", null)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Perform any additional setup after loading the view, typically from a nib.
if (ViewModel == null)
return;
var app = UIApplication.SharedApplication.Delegate as AppDelegate;
app.SidebarController = new SidebarController(this,
CreateViewFor(RootViewModel.Home, false), CreateViewFor(RootViewModel.Menu, true));
app.SidebarController.MenuWidth = 220;
app.SidebarController.ReopenOnRotate = false;
app.SidebarController.MenuLocation = MenuLocations.Left;
}
public override void DidReceiveMemoryWarning()
{
base.DidReceiveMemoryWarning();
// Release any cached data, images, etc that aren't in use.
}
private UIViewController CreateViewFor(IMvxViewModel viewModel, bool navBarHidden)
{
var controller = new UINavigationController();
var screen = this.CreateViewControllerFor(viewModel) as UIViewController;
controller.PushViewController(screen, false);
controller.NavigationBarHidden = navBarHidden;
return controller;
}
答案 0 :(得分:0)
这就是我在我的应用中实现补充工具栏导航的方式,并且工作得非常好。
RootViewController.cs
public class RootViewController : UIViewController
{
UIStoryboard _storyboard;
// the sidebar controller for the app
public SidebarController LeftSidebarController { get; private set; }
// the navigation controller
public NavigationController NavController { get; private set; }
// the storyboard
public override UIStoryboard Storyboard {
get {
if (_storyboard == null)
_storyboard = UIStoryboard.FromName ("Main", null);
return _storyboard;
}
}
public RootViewController () : base (null, null)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
var programController = (CareProgramController)Storyboard.InstantiateViewController ("careProgramController"); // This is the menu..
var leftMenuController = (LeftMenuController)Storyboard.InstantiateViewController ("leftMenuController"); // This is the initial ViewController
NavController = new NavigationController ();
NavController.PushViewController (programController, false);
LeftSidebarController = new SidebarController (this, NavController, leftMenuController) {
HasShadowing = false,
MenuWidth = 280,
MenuLocation = MenuLocations.Left
};
}
}
BaseViewController.cs
public class BaseViewController : UIViewController
{
// provide access to the sidebar controller to all inheriting controllers
protected SidebarNavigation.SidebarController SidebarController {
get {
return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.LeftSidebarController;
}
}
// provide access to the navigation controller to all inheriting controllers
protected NavigationController NavController {
get {
return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.NavController;
}
}
// provide access to the storyboard to all inheriting controllers
public override UIStoryboard Storyboard {
get {
return (UIApplication.SharedApplication.Delegate as AppDelegate).RootViewController.Storyboard;
}
}
public BaseViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
SetupNavigationBar ();
SetDrawerToogle ();
}
void SetupNavigationBar ()
{
if (NavController != null) {
NavController.NavigationBar.BarTintColor = AZConstants.PrimaryColor;
NavController.NavigationBar.Translucent = false;
NavController.NavigationBar.TintColor = UIColor.White;
NavController.NavigationBar.BarStyle = UIBarStyle.Black;
}
}
void SetDrawerToogle ()
{
NavigationItem.SetLeftBarButtonItem (
new UIBarButtonItem (UIImage.FromFile ("ic_menu_white.png").ImageWithRenderingMode (UIImageRenderingMode.AlwaysOriginal)
, UIBarButtonItemStyle.Plain
, (sender, args) => {
SidebarController.ToggleMenu ();
}), true);
}
}
现在我的应用程序中的所有ViewControllers都从BaseViewController扩展,因此它将有一个导航抽屉
AppDelegate.cs
public class AppDelegate : UIApplicationDelegate
{
// class-level declarations
public RootViewController RootViewController { get { return Window.RootViewController as RootViewController; } }
public override UIWindow Window
{
get;
set;
}
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, true);
GotoRootViewController();
return true;
}
public void GotoRootViewController()
{
Window = new UIWindow(UIScreen.MainScreen.Bounds);
//Window.RootViewController.DismissViewController(true, null);
//If you have defined a root view controller, set it here:
Window.RootViewController = new RootViewController();
// make the window visible
Window.MakeKeyAndVisible();
}
答案 1 :(得分:0)
我设法通过在RootView.cs的ViewDidLoad()中添加以下行来修复这种奇怪的行为:
View.Frame = new CGRect(0, 0, 0, 0);
由于某种原因,看起来RootViewController的视图高于其他两个,这是我设法让它工作的唯一方法。如果有人有更好的解决方案,请添加你的答案,我会尝试。我所做的看起来更像是一种解决方法。
@HeisenBerg我们的项目之间存在一个区别,我正在使用MVVMCross,这会更改屏幕之间的流量。但是谢谢你的帮助!