2012-08-09 224 views
1

在我viewDidLoad方法中,我有以下:添加按钮自定义导航栏

navigBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemAction target:self action:@selector(btnClicked:)]; 
[self.view addSubview:navigBar]; 

的按钮不会显示在所有!我错过了什么?

回答

4
// create the navigation bar and add it to the view 
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[self.view addSubview:navigationBar]; 

// create a button 
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonSystemItemAction target:self action:@selector(btnClicked:)]; 

// create a UINavigationItem and add the button in the right hand side 
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil]; 
navItem.rightBarButtonItem = button; 

// add the UINavigationItem to the navigation bar 
[navigationBar pushNavigationItem:navItem animated:NO]; 
3

在导航栏上设置按钮,然后在导航上创建一个BarButtonItem

barbutton=[UIBarButtonItem alloc] ]initWithTitle...... 
navigation.navigationController.rightBarButton= barbutton; 
+3

我真的不明白你在说什么。 – Jordan 2012-08-09 18:44:38

3

首先阅读:

当您使用导航栏作为一个独立的对象,你是负责提供内容。与其他类型的视图不同,您不直接将子视图添加到导航栏。相反,您使用导航项目(UINavigationItem类的一个实例)来指定要显示的按钮或自定义视图。导航项目具有用于指定导航栏左侧,右侧和中心视图以及用于指定自定义提示字符串的属性。

导航栏管理一堆UINavigationItem对象。虽然堆栈主要用于支持导航控制器,但您也可以使用它来实现自己的自定义导航界面。堆栈中最顶端的项目表示其内容当前由导航栏显示的导航项目。使用pushNavigationItem:animated:方法将新的导航项目推送到堆栈上,并使用popNavigationItemAnimated:方法从堆栈中弹出项目。这两个变化都可以为用户带来好处。

所以基本上你需要做的是:

[navigBar pushNavigationItem:self.navigationItem animated:NO];