【问题标题】:Xamarin iOS programmatically add navigation bar to table viewXamarin iOS 以编程方式将导航栏添加到表格视图
【发布时间】:2014-03-18 12:16:09
【问题描述】:

我不想使用故事板。我想以编程方式将导航栏添加到表视图。我得到了一个带有表格视图的 Xib 文件,但其余的都是由代码完成的。

如何添加导航栏并对其进行自定义?

我尝试了以下方法,但不起作用:

UINavigationBar navBar = new UINavigationBar ();
View.AddSubview (navBar);

【问题讨论】:

  • 表视图不应该在 UINavigationView 中而不是相反吗?

标签: c# ios uitableview uinavigationbar xamarin


【解决方案1】:

1 创建一个 UIViewController 的实例,代表必要的屏幕。

2 将 UIViewController 的实例作为参数传递给 UINavigationController 的构造函数。

3 将 windows 根视图控制器设置为 UINavigationController 的实例以显示必要的屏幕:

 [Register ("AppDelegate")] 
 public partial class AppDelegate : UIApplicationDelegate {

     UIWindow window;   
     CompanyController cc;

     public override bool FinishedLaunching (UIApplication app, NSDictionary options)
     {     
        window = new UIWindow (UIScreen.MainScreen.Bounds);   
        cc = new CompanyController ();
        // If you have defined a root view controller, set it here:                   
        window.RootViewController = new UINavigationController (cc);
        // make the window visible     
        window.MakeKeyAndVisible ();
        return true;   
      } 
  }

更多详情http://visualstudiomagazine.com/articles/2014/01/01/xamarin-how-to.aspx

【讨论】: