导航栏的自定义后退按钮

时间:2015-02-02 09:35:14

标签: ios iphone uinavigationcontroller uinavigationbar uinavigationitem

我制作了一个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;
}

帮我解决这个问题!

提前致谢

2 个答案:

答案 0 :(得分:1)

enter image description here

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

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;

答案 1 :(得分:0)

试试这个

 -(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];

    }