Ios 在导航栏上添加自定义后退按钮

Ios 在导航栏上添加自定义后退按钮,ios,iphone,uinavigationbar,Ios,Iphone,Uinavigationbar,我试图在导航栏上添加自定义后退按钮,但它不起作用。下面是我的代码 -(void)addLeftButton { UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"]; UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom]; [aButton setBackgroundImage:buttonImage forState:UICo

我试图在导航栏上添加自定义后退按钮,但它不起作用。下面是我的代码

-(void)addLeftButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

   [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

   self.navigationItem.backBarButtonItem = aBarButtonItem;
}  
请告诉我这个代码有什么问题,试试这个

-(void)addLeftButton
{
     UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

     UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

     [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

     aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

     UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

     [aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];

     [self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}

下面是我用来创建自定义backbutton的代码

- (void)viewDidLoad
    {
        [super viewDidLoad];

    // Set the custom back button

    //add the resizable image
    UIImage *buttonImage = [[UIImage imageNamed:@"backbtn.png"]
                            resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];

    MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
    lbl.text = @"Settings";
    lbl.backgroundColor = [UIColor clearColor];
    //lbl.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lbl.numberOfLines = 0;

    lbl.lineHeight = 12;
    lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
    lbl.font = [UIFont fontWithName:@"Helvetica" size:12];

    [button addSubview:lbl];

    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;

}

-(void)back {
    // Tell the controller to go back
    [self.navigationController popViewControllerAnimated:YES];
}
此代码将调整图像大小,使其适合文本。
您也可以用任何方法放置此按钮。

您没有向按钮添加操作。因此,您需要向按钮添加操作。有关更多信息,请参阅以下代码中的

-(void)addLeftButton
{
    UIImage *buttonImage = [UIImage imageNamed:@"btn_back.png"];

    UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];

    aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width,   buttonImage.size.height);

    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

    [aButton addTarget:self action:@selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----

   self.navigationItem.backBarButtonItem = aBarButtonItem;
}  

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

这是为left button not back button@Developer.iOSit表示您不想将back按钮放在导航的左侧??实际上backButton和leftButton在IOS7中是两个不同的东西设置动作不重要@Dharambir