简体   繁体   English

添加返回按钮到导航栏

[英]Adding back button to navigation bar

I've added a navigation bar to a UIViewController. 我已经为UIViewController添加了一个导航栏。 It is displayed from another UIViewController only. 它仅从另一个UIViewController显示。 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. 我似乎只能通过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. 目前,在RootController中,我通过简单地执行addSubView来推送另一个UIViewController(viewB)。 In viewB, I want to display the navigation bar. 在viewB中,我想显示导航栏。 The app is view based, not navigation controller based. 该应用程序是基于视图,而不是基于导航控制器。

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. 如果您没有使用导航控制器,请查看Three20样式组件以制作自定义栏按钮。

I have done it the following way 我是通过以下方式完成的

In viewDidLoad Method I have this code: 在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];

Then add conformity to UINavigationBarDelegate protocol in the header and implement the delegate method this way: 然后在标头中添加符合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;
}

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: 解决此问题的另一种方法是设置导航栏的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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM