Tom Spee Tom Spee - 1 year ago 175
iOS Question

Xamarin iOS programmatically add navigation bar to table view

I don't want to use a storyboard. I want to programmatically add a navigation bar to a Table View. I got one Xib file with a Table View but the rest is all done by code.

How can I add a navigation bar and customize it?

I tried the following but that doesn't work:

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

Answer Source

1 Create an instance of the UIViewController, representing the necessary screen.

2 Pass the instance of the UIViewController as a parameter to the constructor of the UINavigationController.

3 Set the windows root view controller to the instance of the UINavigationController to show the necessary screen:

 [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;   

more detail

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download