如何更改导航栏上的后退按钮图像?

时间:2013-11-01 11:55:27

标签: ios user-interface uinavigationbar

在故事板中,我使用“嵌入”创建导航栏。

我正在尝试替换后退按钮的原始图像,就像这样

enter image description here

通过我的后退按钮图像,这是

enter image description here

怎么做?

4 个答案:

答案 0 :(得分:2)

在您的控制器viewDidLoad方法中添加以下代码..

UIImage *leftbuttonImage = [UIImage imageNamed:@"yourimagename"];
        UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftbutton setImage:leftbuttonImage forState:UIControlStateNormal];
        leftbutton.frame = CGRectMake(0, 0, 35, 35);
        [leftbutton addTarget:self action:@selector(showLeftMenuPressed:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *customBarleftItem = [[UIBarButtonItem alloc] initWithCustomView:leftbutton];

        self.navigationItem.leftBarButtonItem = customBarleftItem;

答案 1 :(得分:2)

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = backButton;

并宣布goback:

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

答案 2 :(得分:2)

-(void)setUpNAvigationBackBarButton
{

    UIImage* image3 = [UIImage imageNamed:@"icon_back.png"];
    CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:image3 forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(YourMethod which you wanna call on back button
         forControlEvents:UIControlEventTouchUpInside];


    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    [self.navigationItem setLeftBarButtonItem:mailbutton animated:YES];
}

只需在viewdidLoad中调用此函数即可。 我在ma代码中使用了相同的这个功能。工作正常..你可以使用它。

答案 3 :(得分:0)

Swift版本: -

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "HomeLeft@2x")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "HomeLeft@2x")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

将它放在viewDidLoad()