質問

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
役に立ちましたか?

解決 2

Don't add your own nav bar. Just put the view controller as the root of a navigation controller.

And there's no need to override loadView like you are. And don't setup your app's root view like you do.

Try the following:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _viewController = [[RootViewController alloc] init];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
    _window.rootViewController = nc;
    [_window makeKeyAndVisible];
}


@implementation RootViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor redColor];

    self.title = @"Title1";
}
@end

他のヒント

The problem is with:

self.navigationBar.topItem.title = @"Title2";

because topItem is nil until you add a UINavigationItem by setting items on the nav bar. The UINavigationItem is where the title should be set and then the nav bar will display it.

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title2"];

self.navigationBar.items = @[ item ];

You are trying to set your viewController's title. It will only work if your viewController is in UINavigationController's stack.

self.title = @"Title2"; //Will not work if you are trying to use your own NavigationBar.

you should use something like this:-

navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 64)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title 2"];
[myNavigationBar pushNavigationItem:item animated:NO];
[self.view addSubview:myNavigationBar];

That's how you can set the title for your navigationBar. If you want barButtonItem also then you should add them in leftBarButtonItem & rightBarButttonItem of UINavigationItem property.

Try:

self.navigationItem.title = "myTitle"
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top