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

时间:2013-11-01 05:03:05

标签: ios iphone uinavigationbar

我正在尝试在导航栏上添加自定义后退按钮,但下面的代码不行,是我的代码

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

请告诉我们这段代码有什么问题

3 个答案:

答案 0 :(得分:5)

试试这个。

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

答案 1 :(得分:0)

以下是我用来创建自定义后退按钮的代码

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

此代码将调整图像大小,使其适合文本。 您也可以将此按钮放在任何方法中。

答案 2 :(得分:0)

您没有为按钮添加操作。因此,您需要为按钮添加操作。有关详细信息,请参阅以下代码..

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