-1

在情节提要中,我使用“嵌入”创建了一个导航栏。

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

在此处输入图像描述

通过我的后退按钮图像,即

在此处输入图像描述

这个怎么做?

4

4 回答 4

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;
于 2013-11-01T12:04:52.817 回答
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;

并声明回退:

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}
于 2013-11-01T12:16:31.217 回答
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 代码中使用了相同的这个函数。工作正常..你可以使用它。

于 2013-11-01T12:16:53.193 回答
0

斯威夫特版本:-

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( )

于 2015-10-19T09:11:08.273 回答