在UINavigationBar上为后退按钮设置自定义图像

时间:2012-06-13 17:10:18

标签: iphone objective-c xcode uinavigationcontroller uibutton

我正在尝试为后退按钮设置自定义图像,当将新视图推入堆栈时,该按钮会自动放置到导航栏上。

我尝试在父viewController的viewDidLoad中添加以下内容:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

我也尝试了以下内容:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"BackButton.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
    self.navigationItem.backBarButtonItem = btn;

使用UIAppearance会产生非常奇怪的结果: The BackButton image seems to be placed underneath the original title

7 个答案:

答案 0 :(得分:36)

试试这段代码

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = backButton;

然后像这样定义goback方法

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

答案 1 :(得分:21)

这是我正在使用的代码,并且在我自己的iOS 5应用程序中运行良好。此代码来自应用委托中的application:didFinishLaunchingWithOptions:

UIImage * backButtonImage = [UIImage imageNamed: @"back-button-image"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

您可能需要使用可伸缩的图像,其中左侧的“点”是左帽


或在Swift中

let backButtonImage = UIImage(named: "back-button-image")
backButtonImage = backButtonImage?.stretchableImageWithLeftCapWidth(15, topCapHeight: 30)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)

答案 2 :(得分:4)

iOS 8+ Swift版本(可以毫不费力地转换为ObjC)。此方法保留了与标准后退按钮相关的导航栏的所有优点和行为(推/弹出动画,交互式弹出手势等)

// Call the code ONE TIME somewhere on app launch to setup custom back button appearance
UINavigationBar.appearance().tintColor = UIColor.blackColor()
// If you need custom positioning for your back button, in this example button will be 1 px up compared to default one
// Also only vertical positioning works, for horizontal add offsets directly to the image
let backImageInsets = UIEdgeInsetsMake(0, 0, -1, 0)
// Get image, change of rendering (so it preserves offsets made in file), applying offsets
let backImage = UIImage(named: "YourButtonImageAssetFileName")?.imageWithRenderingMode(.AlwaysOriginal).imageWithAlignmentRectInsets(backImageInsets)
// Setting images
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage

// Call EACH TIME BEFORE pushing view controller if you don't need title near your back button arrow
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
// Push controller
self.navigationController?.pushViewController(vc, animated: true)

答案 3 :(得分:3)

尝试下面的代码,它对我有用:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];

答案 4 :(得分:0)

这对我有用:

let button1 = UIBarButtonItem(
    image: #imageLiteral(resourceName: "back_arrow"), 
    style: .plain, 
    target: self, 
    action: #selector(self.backBtnTapped(_:))
)
self.navigationItem.leftBarButtonItem  = button1

答案 5 :(得分:0)

此代码适用于 Swift 4 XCode 9.4

将此代码添加到AppDelegate的func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool中以应用于所有视图控制器:

//Custom back button
    let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
    let backImg: UIImage = UIImage(named: "back")!
    barButtonItemAppearance.setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)

    let image = UIImage()

    UINavigationBar.appearance().backIndicatorImage = image
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = image

答案 6 :(得分:0)

将此方法写在普通类中,然后从要绘制的控制器中调用它。

(UIButton *)drawNavigationBarBackButton:(UIViewController*)viewController 
{

    UIImage *navBackImage = nil;

    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
     navBackImage = [UIImage imageNamed:@"Back_Button"];

    [backButton setImage:navBackImage forState:UIControlStateNormal];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    viewController.navigationItem.leftBarButtonItem = leftButton;

    return backButton;
}



call from controller class and setting action

- (void)viewDidLoad {

[super viewDidLoad];

 UIButton *backButton = [CommonClass drawNavigationBarBackButton:self];
    [backButton addTarget:self action:@selector(onTapBackButton) forControlEvents:UIControlEventTouchUpInside];
}

- (void)onTapBackButton 
{
    [self.navigationController popViewControllerAnimated:YES];
}