向左箭头 - UINavigationItem
我四处搜寻,似乎无法弄清楚。我确信很多人都会有我的链接等,我很可能已经看过了。但如果有人可以,请向我展示执行以下操作的代码:
我希望在 UINavigationBar
中有一个左箭头作为“后退”UINavigationItem
。我该怎么做?这是我的 UIViewController
中当前的代码:
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonSelected:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:nil action:@selector(optionsButtonSelected:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;
[self.view addSubview:theBar];
I've searched around and I can't seem to figure it out. I'm sure many people will have links for me and such, which I've most likely already looked at. But if someone could please just show me code to do the following:
I'd like to have a left arrow in my UINavigationBar
as a "Back" UINavigationItem
. How can I do this? Here is my current code in my UIViewController
:
theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonSelected:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:nil action:@selector(optionsButtonSelected:)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;
[self.view addSubview:theBar];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这可能就是您正在寻找的。
I think this may be what you're looking for.
您应该使用
UINavigationController
以便在屏幕上弹出/推送UIViewControllers
。导航控制器会自动将UINavigationBar
添加到您的UIViewControllers
中,因此您无需像以前那样创建它们。这是一个示例。我没有寻找内存泄漏。
在应用程序委托中,您将找到以下方法:
MainVC 是一个 UIViewController,代表层次结构的第 1 级。其中我有
SecondVC 是另一个 UIViewController ,代表层次结构的第二层。在这里您将找到您想要的后退按钮。
You should use an
UINavigationController
in order to pop/pushUIViewControllers
on your screen. The navigation controller will add anUINavigationBar
to yourUIViewControllers
automatically so you will not need to create them as you did.Here is a sample. I didn't looked for memory leaks.
In the app delegate you'll find this method:
MainVC is a
UIViewController
that represents the level 1 of the hierarchy. in it i haveSecondVC is another
UIViewController
that represents the second level of the hierachy. Here you will find the back button that you want.