14

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

我尝试在父视图控制器的 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 会产生非常奇怪的结果: BackButton 图像似乎位于原始标题下方

4

8 回答 8

37

试试这个代码

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];
}
于 2012-06-13T18:27:47.713 回答
29

Since iOS 7.0 there is a new method in API backIndicatorImage. You can use it instead of setBackButtonBackgroundImage if you don't want the image to be stretched to fit the text (for example if you want a fixed size custom back arrow). Here's an example in swift:

let image = UIImage(named: "back_button")
    
UINavigationBar.appearance().backIndicatorImage = image
UINavigationBar.appearance().backIndicatorTransitionMaskImage = image

You can hide the text from the button using this trick:

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default)

for Swift 5

var backButtonImage = UIImage(named: "back_button")

backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 15, topCapHeight: 30) UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)

于 2015-12-20T00:34:10.773 回答
23

这是我正在使用的代码,可以在我自己的 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];

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


或在斯威夫特

let backButtonImage = UIImage(named: "back-button-image")
backButtonImage = backButtonImage?.stretchableImageWithLeftCapWidth(15, topCapHeight: 30)
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)
于 2012-06-13T23:31:32.623 回答
5

iOS 8+ Swift version (can be converted to ObjC with not much effort). This method preserves all advantages and behaviour of navigation bar related to standard back button (animation on pushing/popping, interactive pop gesture, etc)

// 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)
于 2016-06-11T18:09:28.577 回答
3

Try the code below, it works for me:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
于 2016-03-31T08:38:35.387 回答
2

此代码适用于Swift 5XCode 10

将此代码添加到 AppDelegatefunc 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
于 2018-07-03T04:34:37.737 回答
2

将此方法写在公共类中,并从要在其中绘制的控制器中调用它。

(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];
}
于 2019-06-11T12:41:23.037 回答
1

这对我有用:

let button1 = UIBarButtonItem(
    image: #imageLiteral(resourceName: "back_arrow"), 
    style: .plain, 
    target: self, 
    action: #selector(self.backBtnTapped(_:))
)
self.navigationItem.leftBarButtonItem  = button1
于 2017-05-04T11:44:19.350 回答