1

我有UITableView基础应用程序,我无法backBarButtonItem正确设置。后退按钮是按钮,当我选择任何项目时出现,使用方法调用第二个视图didSelectRowAtIndexPath。当我尝试这样设置时:

UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
    [back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
    [back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
    [back1 setImage:[UIImage imageNamed:@"back_button.png"] forState:UIControlStateNormal];
    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
    self.navigationItem.backBarButtonItem = back;

只有具有默认样式的后退按钮,并且以某种方式带有我的TableView控制器窗口的标题。

当我这样设置时:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                               initWithImage:[UIImage imageNamed:@"back_button.png"]
                               style:UIBarButtonItemStyleBordered 
                               target:nil 
                               action:nil];

 self.navigationItem.backBarButtonItem = backButton;

有一个图像,但是,它看起来像默认后退按钮中的“插入”,看起来一点也不好看。

请帮助我.. 我尝试了很多方法来解决这个问题,但是,在许多情况下,只有默认的返回按钮,标题为“产品”(我的导航栏顶部出现了标题UITableView)。任何帮助,将不胜感激!

4

3 回答 3

2
   UIImage *buttonImage = [UIImage imageNamed:@"back_button.png"];
   UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
  [backButton setTitle:@"Back" forState:UIControlStateNormal];
  backButton.font = [UIFont boldSystemFontOfSize:12];
 backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
 [backButton addTarget:self action:@selector(showMeter) 
       forControlEvents:UIControlEventTouchUpInside];

 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] 
       initWithCustomView:backButton];
于 2012-07-03T07:28:01.370 回答
1
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:@"yourButton"] 
        resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];


//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal 
    barMetrics:UIBarMetricsDefault];
于 2012-07-04T06:05:48.827 回答
0

You should assign it to self.navigationItem.leftBarButtonItem.

self.navigationItem.leftBarButtonItem = backButton;
于 2012-07-03T07:27:50.993 回答