如何以编程方式创建UINavigationBar

时间:2013-11-04 06:13:45

标签: ios objective-c navigationbar

我最近开始学习Obj-C并且根据我的朋友(对Obj-C有经验),如果我学会了如何以编程方式创建视图而不依赖于StoryBoards,那就更好了。

所以......我一直在尝试用左键创建一个NavigationBar。但是,当我运行它时,我得到的是实际的NavigationBar,而不是按钮。这是我的代码:

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]
               initWithFrame:[[UIScreen mainScreen] bounds]];


firstController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:firstController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

这是MainViewController.m:

    - (void)viewDidLoad
{
[super viewDidLoad];

UIColor *navbarColor = [UIColor whiteColor];

self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = navbarColor;


UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:NULL];

self.navigationController.navigationItem.leftBarButtonItem = button;

}

任何帮助将不胜感激。我是Objective-C的新手,但我理解代码概念,因此在解释^时你不需要“宝贝”说话。^

1 个答案:

答案 0 :(得分:1)

[self.window makeKeyAndVisible];

之前添加此行
[self.window addSubview:navController.view];