2

我想在导航栏中更改后退按钮的后退按钮外观。为此,我有这段代码。

UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = @"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
                                                 green:158/255.0
                                                  blue:218/255.0
                                                 alpha:1.0];

backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;

但我仍然得到标准的后退按钮。有什么帮助吗?

4

5 回答 5

1

在将该 viewController 推送到堆栈之前,将您的代码自定义后退按钮。并设置 BackBarButtonItem ,而不是 leftBarButtonItem。

UIImage *image = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[backbutton setImage:image forState:UIControlStateNormal];
backbutton.titleLabel.textColor = [UIColor blueColor];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[[self navigationItem] setBackBarButtonItem: back];
[self.navigationController pushViewController:yourViewControllerObject animated:YES];
于 2012-12-13T09:13:58.257 回答
0

采用

 self.navigationItem.leftBarButtonItem = back;

代替

 self.navigationController.navigationItem.leftBarButtonItem = back;
于 2012-12-13T09:10:01.543 回答
0

根据您的要求使用以下代码..

- (void)viewDidLoad
{
        ///write your code

        UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
        UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        //[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
        [backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
        [backbutton setTitle:@"Back" forState:UIControlStateNormal];
        backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
        UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
        [backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = back;
}

当后退按钮单击下面的方法调用时,您将转到上一个视图控制器

-(void)back_Clicked{
      [self.navigationController popViewControllerAnimated:YES];
}
于 2012-12-13T09:11:05.253 回答
0
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[self buttonWithImage:@"backButton" heighlightImageName:@"backButton" buttonFrame:CGRectMake(2, 1, 30, 30) selectorName:@selector(buttonBackClicked:) target:self]]];


- (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [tempButton setFrame:buttonFrame];
    [tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
    [tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
    [tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
    return tempButton;
}
- (UIImage*)getImageFromResource:(NSString*)imageName
{
    return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
}
于 2012-12-13T09:17:35.240 回答
0
   IButton *favButton = [[UIButton alloc] init];

     [favButton setImage:[UIImage imageNamed:@"unselected.png"]     forState:UIControlStateNormal];
     [favButton addTarget:self action:@selector(favouriteButtonClicked:)
    forControlEvents:UIControlEventTouchUpInside];

 UIBarButtonItem *button = [[UIBarButtonItem alloc]
                            initWithCustomView:favButton];

 self.navigationItem.rightBarButtonItem = button;

  [button release];
  [favButton release];
于 2012-12-13T09:37:16.743 回答