I would like to add a UINavigationBar
to my root view and set the title on the navigation bar.
I have tried using a lot of answers from other similar questions, but none of them worked.
I'm creating the whole view hierarchy programmatically (because I'm using theos). The UINavagationBar
is visible when launching the app, however it has no title, even though I'm both setting the title of the root view controller and the title of the navigation bars top item.
My application will only have one main view, so the navigation bar is only there for aesthetic reasons.
The full source code of the application is below the line.
main.m:
int main(int argc, char **argv) {
@autoreleasepool {
return UIApplicationMain(argc, argv, @"CapabilitiesEditorApplication", @"CapabilitiesEditorApplication");
}
}
CapabilitiesEditorApplication.m:
#import "RootViewController.h"
@interface CapabilitiesEditorApplication: UIApplication <UIApplicationDelegate> {
UIWindow *_window;
RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end
@implementation CapabilitiesEditorApplication
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_viewController = [[RootViewController alloc] init];
[_window addSubview:_viewController.view];
[_window makeKeyAndVisible];
}
@end
RootViewController.h:
@interface RootViewController: UIViewController
@property UINavigationBar *navigationBar;
@end
RootViewController.m:
#import "RootViewController.h"
@implementation RootViewController
- (void)loadView {
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
self.view = [[UIView alloc] initWithFrame:appFrame];
self.view.backgroundColor = [UIColor redColor];
self.title = @"Title1";
self.navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, appFrame.size.width, 44)];
self.navigationBar.topItem.title = @"Title2";
[self.view addSubview:self.navigationBar];
}
@end