我想在我的根视图中添加UINavigationBar
并在导航栏上设置标题。
我尝试过使用其他类似问题的很多答案,但都没有奏效。
我以编程方式创建整个视图层次结构(因为我使用了theos)。启动应用时UINavagationBar
是可见的,但它没有标题,即使我同时设置了根视图控制器的标题和导航栏顶部项的标题。
我的应用程序只有一个主视图,因此导航栏只是出于审美原因。
该应用程序的完整源代码位于该行之下。
的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
答案 0 :(得分:5)
问题在于:
self.navigationBar.topItem.title = @"Title2";
,因为topItem
为nil
,直到您通过在导航栏上设置UINavigationItem
来添加items
。 UINavigationItem
是应设置title
的位置,然后导航栏会显示它。
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title2"];
self.navigationBar.items = @[ item ];
答案 1 :(得分:2)
不要添加自己的导航栏。只需将视图控制器作为导航控制器的根目录即可。
并且不需要像你一样覆盖loadView
。并且不要像你那样设置应用程序的根视图。
尝试以下方法:
- (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
答案 2 :(得分:0)
您正在尝试设置viewController的标题。它只有在你的viewController在UINavigationController的堆栈中时才有效。
self.title = @"Title2"; //Will not work if you are trying to use your own NavigationBar.
你应该使用这样的东西: -
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];
这就是你可以为navigationBar设置标题的方法。如果你想要barButtonItem,那么你应该将它们添加到leftBarButtonItem&amp; rightBarButttonItem of UINavigationItem property。
答案 3 :(得分:0)
尝试:
self.navigationItem.title = "myTitle"