我想在我的根视图中添加UINavigationBar
并在导航栏上设置标题。 我曾尝试使用很多来自其他类似问题的答案,但他们都没有工作。无法设置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