UINavigationItem BackButtonItem 不显示
使用下面的代码我正在实现我自己的导航栏。由于某种原因,当我运行我的应用程序时,导航栏上的后退(左箭头)按钮没有显示任何内容。但是,如果我将代码更改为 leftBarButtonItem,该按钮就会出现。
// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];
UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
navigationItem.backBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];
Using the below code I am implementing my own Navigation Bar. For some reason when I run my app nothing is showing up for a back (left-arrow) button on the navigation bar. However if I change the code to leftBarButtonItem, the button does appear.
// Draw Navigation Bar
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navigationBar setDelegate:self];
UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
navigationItem.backBarButtonItem = backButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];
[navigationBar release];
[backButton release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
backBarButtonItem
由父 ViewController 设置。换句话说,它不是由您看到它的
ViewController
设置的,而是由它指向的ViewController
设置的。因此,如果您的ViewController
是第一个,它就不会有后退按钮。另外,您正在自己创建一个 NavigtionBar。这通常不是正确的方法,
UINavigationBar
不是像按钮或标签那样的 UI 元素。您应该使用UINavigationController
来处理ViewControllers
的所有推送和弹出操作。The
backBarButtonItem
is set by the parent ViewController.In other words, it's not set by the
ViewController
on which you see it but by theViewController
to which it points. So if yourViewController
is the first in line, it just won't have a back button.Also, you are creating a NavigtionBar by yourself. This is usually not the way to go, a
UINavigationBar
isn't a UI-Element like a Button or a Label. You should rather use aUINavigationController
to handle all the pushing and popping of yourViewControllers
.想通了:
Figured it out: