我正在尝试在导航栏中使用自定义项目作为后退按钮。
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem: customItem];
[customItem release];
我最终得到的是我的图像周围有边框。它看起来像这样(我的图像是后退按钮):
我怎样摆脱边界?我做错了什么?
答案 0 :(得分:10)
您的图片显示在后退按钮内部,显然(来自您的屏幕截图)与后退按钮的尺寸不同。
您可能希望隐藏后退按钮,然后将其替换为“左栏按钮”。
<强>代码:强>
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:backButtonImage style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(popViewControllerAnimated:)];
[self.navigationController setHidesBackButton:YES];
[self.navigationItem setLeftBarButtonItem: customItem];
[customItem release];
答案 1 :(得分:10)
以Jorge的代码为基础,这是我的解决方案。
我在UIViewController
上创建了一个简单类别:
<强>的UIViewController + ImageBackButton.h 强>
#import <UIKit/UIKit.h>
@interface UIViewController (ImageBackButton)
- (void)setUpImageBackButton;
@end
<强>的UIViewController + ImageBackButton.m 强>
#import "UIViewController+ImageBackButton.h"
@implementation UIViewController (ImageBackButton)
- (void)setUpImageBackButton
{
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 34, 26)];
[backButton setBackgroundImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
UIBarButtonItem *barBackButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
[backButton addTarget:self action:@selector(popCurrentViewController) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = barBackButtonItem;
self.navigationItem.hidesBackButton = YES;
}
- (void)popCurrentViewController
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
现在,您需要做的只是#import UIViewController+ImageBackButton.h
所有视图控制器或其他视图控制器继承的自定义基本视图控制器类并实现viewWillAppear:
方法:
- (void)viewWillAppear:(BOOL)animated
{
[self setUpImageBackButton];
}
这就是全部。现在到处都有一个图像后退按钮。没有边界。享受!
答案 2 :(得分:6)
这是一个更新版本。这包括设置目标,字体大小等。
此外,它反映setHidesBackButton
不可用作navigationController
的属性。
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonBackgroundImg = [UIImage imageNamed:@"BackButton.png"];
backButton.frame = CGRectMake(0.0f, 0.0f, backButtonBackgroundImg.size.width, backButtonBackgroundImg.size.height);
[backButton setBackgroundImage:backButtonBackgroundImg forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(didTouchUpInsideBackButton:) forControlEvents:UIControlEventTouchUpInside];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:[UIFont smallSystemFontSize]];
[backButton setTitle:@"MyTitle" forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
请注意,这是来自ARC项目,因此对象上没有任何版本等。
答案 3 :(得分:1)
我创建了一个UINavigationBar类,我在每个viewControllers中调用viewWillAppear。我用来更改后退按钮外观的代码如下:
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake( your button frame)];
[backButton setBackgroundImage:[UIImage imageNamed:@"your image name"] forState:UIControlStateNormal];
UIBarButtonItem *barBackButtonItem = [[UIBarButtonItem alloc] initWithCustomView: backButton];
[backButton addTarget:delegate action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
self.topItem.leftBarButtonItem = barBackButtonItem;
self.topItem.hidesBackButton = YES;
在iOS 6下完美运行。