如何使leftBarButtonItem看起来像backBarButtonItem?

时间:2011-09-13 06:53:39

标签: ios uinavigationcontroller uinavigationbar uinavigationitem backbarbuttonitem

默认解决方案不合适:

  1. 更改以前的ViewController标题 - 我需要让我自己的功能控制按钮触及
  2. 制作一个leftBarButtonItem并隐藏backBarButtonItem - leftBarButtonItem看起来不像默认的backBarButtonItem。

4 个答案:

答案 0 :(得分:13)

以下是创建自定义leftBarButtonItem的示例:

UIImage *buttonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *aButton = [UIbutton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0,0.0,buttonImage.size.width,buttonImage.size.height);
[aButton addTarget:self action:@selector(aSelector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
self.navigationItem.leftButtonItem = backButton;

希望它有所帮助...

修改

试试这个......

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourimage.jpg"] style:UIBarButtonItemStyleBordered target:self action:@selector(aSelector)];
self.navigationItem.leftBarButtonItem = backButton;

我不记得按钮是什么类型的按钮,尝试将默认样式更改为其他按钮。祝好运!

答案 1 :(得分:0)

只需创建一个自定义barbuttonitem并使用类似于后退按钮项的图片进行自定义。 您可以从导航堆栈中的视图控制器获取其文本。

然后处理水龙头以实现所需的效果

答案 2 :(得分:0)

您可以将自定义视图作为条形按钮分配给导航控制器,如下所示:

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomizedControl];
[yourCustomizedControl release];

self.navigationItem.leftBarButtonItem = customItem;
[customItem release];

如果需要,请查看this Apple documentation以获取更详细的说明。

答案 3 :(得分:0)

这是一个添加后退按钮的黑客:

UINavigationItem* backNavigationItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[_navigationBar pushNavigationItem:backNavigationItem animated:NO];
[backNavigationItem release];

您可以这样添加更多项目(例如标题,右键):

UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title"];
navigationItem.rightBarButtonItem = // Some UIBarButtonItem
[_navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];