向导航栏添加后退按钮

发布于 2024-09-01 18:12:38 字数 276 浏览 4 评论 0原文

我已向 UIViewController 添加了一个导航栏。它仅从另一个 UIViewController 显示。我想要一个形状类似于箭头的左侧后退按钮,就像普通的导航栏后退按钮一样。看来只能通过IB添加一个bar按钮了。我猜后退按钮需要以编程方式添加。关于我应该如何执行此操作有什么建议吗?

目前,在 RootController 中,我通过简单地执行 addSubView 来推送另一个 UIViewController (viewB)。在viewB中,我想显示导航栏。该应用程序是基于视图的,而不是基于导航控制器的。

I've added a navigation bar to a UIViewController. It is displayed from another UIViewController only. I'd like to have a left side back button that is shaped similar to an arrow, just like the normal navigation bar back button. It seems I can only add a bar button through IB. I'm guessing the back button needs to be added programmatically. Any suggestions on how I should do this?

Currently, in the RootController, I push another UIViewController (viewB) by simply doing an addSubView. In viewB, I want to display the navigation bar. The app is view based, not navigation controller based.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2024-09-08 18:12:38

如果您使用的是导航控制器:

MyViewController *_myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[[self navigationController] pushViewController:_myViewController animated:YES];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[_backButton release], _backButton = nil;
[_myViewController release], _myViewController = nil;

如果您没有使用导航控制器,请查看 Three20 样式 用于制作自定义栏按钮的组件。

If you're using a navigation controller:

MyViewController *_myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[[self navigationController] pushViewController:_myViewController animated:YES];
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = _backButton;
[_backButton release], _backButton = nil;
[_myViewController release], _myViewController = nil;

If you're not using a navigation controller, look into the Three20 style components to make custom bar buttons.

給妳壹絲溫柔 2024-09-08 18:12:38

我已经按照以下方式完成了

在 viewDidLoad 方法中我有以下代码:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 41)];
    navBar.delegate = self;

    UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
    [navBar pushNavigationItem:backItem animated:NO];
    [backItem release];

    UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Your Title"];
    [navBar pushNavigationItem:topItem animated:NO];
    topItem.leftBarButtonItem = nil;
    [topItem release];

    [self.view addSubview:navBar];
    [navBar release];

然后在标头中添加对 UINavigationBarDelegate 协议的一致性并以这种方式实现委托方法:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    //if you want to dismiss the controller presented, you can do that here or the method btnBackClicked

    return NO;
}

I have done it the following way

In viewDidLoad Method I have this code:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 41)];
    navBar.delegate = self;

    UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
    [navBar pushNavigationItem:backItem animated:NO];
    [backItem release];

    UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Your Title"];
    [navBar pushNavigationItem:topItem animated:NO];
    topItem.leftBarButtonItem = nil;
    [topItem release];

    [self.view addSubview:navBar];
    [navBar release];

Then add conformity to UINavigationBarDelegate protocol in the header and implement the delegate method this way:

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
    //if you want to dismiss the controller presented, you can do that here or the method btnBackClicked

    return NO;
}
命比纸薄 2024-09-08 18:12:38

解决此问题的另一种方法是设置导航栏的 items 属性,而不是将栏项目连续推入导航栏堆栈:

//Define myFrame based on your needs
let navigationBar = UINavigationBar(frame: myFrame)
let backItem = UINavigationItem(title: "Back")
let topItem = UINavigationItem(title: "My Title")
navigationBar.setItems([backItem,topItem], animated: false)

Another approach to solve this problem is to set the items property for the navigation bar instead of consecutively pushing the bar items into nav bar stack:

//Define myFrame based on your needs
let navigationBar = UINavigationBar(frame: myFrame)
let backItem = UINavigationItem(title: "Back")
let topItem = UINavigationItem(title: "My Title")
navigationBar.setItems([backItem,topItem], animated: false)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文