导航栏的自定义后退按钮

Custom Back Button for Navigation Bar

我做了一个UINavigationBar的分类如下:

UINavigationBar (UINavBar_Category)

我想用这个创建自定义后退按钮。

我可以通过以下代码设置图像:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

问题无法更改后退按钮的标题。

我的尝试

 - (void)didMoveToSuperview
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:lang(@"btnBack") style:UIBarButtonItemStylePlain target:nil action:nil];
    self.topItem.backBarButtonItem=backButton;
}

帮我解决这个问题!

提前致谢

请尝试使用此代码。这里首先创建了一个按钮,设置了它的标题和图像,然后将其设置为导航控制器的左栏按钮项。

UIButton *backBarButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 29)];

[backBarButton setTitle:@"  Log Out" forState:UIControlStateNormal];
                [backBarButton setImage:[UIImage 
imageNamed:@"leftarrow_ipad.png"] forState:UIControlStateNormal];

[backBarButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBarButton];

self.navigationItem.leftBarButtonItem = backBarButtonItem;

试试这个

 -(void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
     if([self.navigationController.viewControllers objectAtIndex:0] != self)
        {
            UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 26)];
            [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; // some image
            [backButton setShowsTouchWhenHighlighted:TRUE];
            [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
            UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
            self.navigationItem.hidesBackButton = TRUE;
            self.navigationItem.leftBarButtonItem = barBackItem;

        }
     }
    -(void)popViewControllerWithAnimation
    {
        [self.navigationController popViewControllerAnimated:YES];

    }