2

我想更改 UINavigationBar 的后退按钮

我可以使用以下代码来做到这一点:

 // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO;


    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 30, 30);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    self.navigationItem.hidesBackButton = YES;

    // Cleanup
    [customBarItem release];

但我必须将该代码放在每个 viewDidLoad 方法中。我想为整个程序做一次。

我的尝试是这样的:

 UIImage *buttonBack30 = [[UIImage imageNamed:@"backag"] //16 5
                             resizableImageWithCapInsets:UIEdgeInsetsMake(1000, 1000, 1000, 1000)];
    UIImage *buttonBack31 = [[UIImage imageNamed:@"selback"] 
                             resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack30 
                                                      forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    //[[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
    //                forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:buttonBack31 
                                                      forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
    [attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
    // [attributes setValue:[UIFont fontWithName:@"Helvetica" size:15] forKey:UITextAttributeFont];
    [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateHighlighted];

但它会拉伸图像并在其上绘制文本,这是我不想要的。我只想要每个视图中大小相同的图像。

谢谢!

4

5 回答 5

23

从 iOS 5.0 开始,您可以使用全局外观修饰符来更改整个应用程序中的所有后退按钮:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
                                        forState:UIControlStateNormal
                                      barMetrics:UIBarMetricsDefault];

背景图像必须是可调整大小的图像才能获得良好的效果。

于 2012-11-15T09:17:16.133 回答
4

这可能会奏效。在顶部的您的 appdelegate 中添加以下内容。

    @implementation UIViewController (CustomFeatures)
-(void)setNavigationBar{
    // Set the custom back button
    UIImage *buttonImage = [UIImage imageNamed:@"backag.png"];

    //create the button and assign the image
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:buttonImage forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"selback.png"] forState:UIControlStateHighlighted]; 
    button.adjustsImageWhenDisabled = NO;


    //set the frame of the button to the size of the image (see note below)
    button.frame = CGRectMake(0, 0, 30, 30);

    [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

    //create a UIBarButtonItem with the button as a custom view
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = customBarItem;
    self.navigationItem.hidesBackButton = YES;

    // Cleanup
    [customBarItem release];
}
@end

并打电话给[self setNavigationBar];viewDidLoad

于 2012-04-23T06:27:07.373 回答
1

使用此代码并享受...

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[leftButton setUserInteractionEnabled:NO];
[leftButton setImage:[UIImage imageNamed:@"backag.png"] forState:UIControlStateNormal];    
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:@selector(YourclickeventClick:) forControlEvents:UIControlEventTouchUpInside];        
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftbutton release];

希望对你有帮助..

于 2012-04-23T06:23:24.417 回答
0

您可以为 backBarButtonItem 创建一个自定义按钮,然后对其进行自定义。

转到这个问题 -如何更改默认 backBarButtonItem 的颜色/图像?你可以在那里找到你的答案。

要使条形按钮项看起来像 backBarButtonItem,请参阅此答案。在 UIToolbar 上创建左箭头按钮(如 UINavigationBar 的“后退”样式)

于 2012-11-15T10:19:37.263 回答
0

这是Swift的工作解决方案

    let backImage = UIImage(named: "back")
    UINavigationBar.appearance().backIndicatorImage = backImage
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
    UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
        UIColor.white], for: .normal)
    UIBarButtonItem.appearance().tintColor = UIColor.green //if you want to change color
于 2019-04-23T10:11:13.457 回答