自定义“按下” UIBarButtonItem 背景

Posted

技术标签:

【中文标题】自定义“按下” UIBarButtonItem 背景【英文标题】:Custom "Pressed" UIBarButtonItem Backgrounds 【发布时间】:2013-01-10 03:33:18 【问题描述】:

我正在尝试为我的 UIBarButtonItem 设置自定义按下的图像,但似乎没有任何效果。根据我的理解,下面的代码应该可以工作,但是尽管将图像设置为突出显示状态,但按钮在按下时看起来完全一样。

有什么想法吗?

UIImage *barButtonBackground = [[UIImage imageNamed:ANBarButtonItemBackgroundImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];
UIImage *barButtonPressedBackground = [[UIImage imageNamed:ANBarButtonPressedImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];

[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

【问题讨论】:

您是如何创建按钮的?您实际上可以看到哪张图片? 你确定按下的图像实际上是不同的吗?尝试交换图像。 我检查了两张图片——它们是不同的。这些按钮是在我的故事板中创建的。 IBActions、icons 和 sofourth 都适用于 bar 按钮项——我只是无法在突出显示时为这些 BarButtonItems 显示备用图像。 @AaronNg :您是否使用过我的答案中提供的自定义按钮方法? 这是用什么方法?你确定你的leftBarButtonItemrightBarButtonItem 已经设置了吗? 【参考方案1】:

我认为您应该为与您的配色方案匹配的不同控制状态创建一个带有背景图像custom UIButton,然后使用这个UIButton作为视图custom UIBarButtonItem

UIButton *customButton = [UIButton buttonWithType:...];

[customButton setBackgroundImage:barButtonBackground  forState:UIControlStateNormal];
[customButton setBackgroundImage:barButtonPressedBackground  forState:UIControlStateSelected];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton];

如果您想要其他参考资料,那么您可以通过这个漂亮的链接:Using Appearance Proxy to Style Apps

【讨论】:

我真的希望有一种简单的方法可以使用实际的 UIBarButtonItems 来做到这一点,但这看起来是我能找到的最好的方法。谢谢。 这应该可以,但是如果我们想为不同的状态设置不同的图像(不是背景图像)怎么办? UIBarButtonItem 上的 setImage 方法似乎没有 forState 方法...我需要更改图像本身,而不仅仅是背景。对此有何想法? 和stan一样的问题??【参考方案2】:

你可以试试这样。对我有用。

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];

【讨论】:

【参考方案3】:

也许这会对您有所帮助(只需将该文件添加到项目中):

UIBarButtonItem+customLook.h

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (customLook)

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image selectedBackGroundImage:(UIImage *)selected target:(id)target action:(SEL)selector;

@end

UIBarButtonItem+customLook.m

#import "UIBarButtonItem+customLook.h"

@implementation UIBarButtonItem (customLook)

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image selectedBackGroundImage:(UIImage *)selected target:(id)target action:(SEL)selector

    UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [someButton setImage:image forState:UIControlStateNormal];
    [someButton setBackgroundImage:selected forState:UIControlStateSelected];

    CGSize size = image.size;
    CGRect frame = CGRectZero;
    frame.size.width = size.width;
    frame.size.height = 30.0f; // Standart height
    someButton.frame = frame;

    [someButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];

    return backButton;


@end

一些视图控制器:

#import "UIBarButtonItem+customLook.h"
...

- (void)viewDidLoad

    [super viewDidLoad];

    UIBarButtonItem *bbi = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"29-heart-selected.png"]
                                          selectedBackGroundImage:[UIImage imageNamed:@"DarkButton_Pressed.png"]
                                                            target:self
                                                            action:@selector(someAction:)];
    self.navigationItem.rightBarButtonItem = bbi;

...

- (void)someAction:(id)sender

    [(UIButton *)sender setSelected:YES];

【讨论】:

【参考方案4】:
    UIImage* btnImage = [UIImage imageNamed:@"share-icon-deselected-16x16.png"];
    CGRect frameimg = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(share:)
         forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    self.navigationItem.rightBarButtonItem=mailbutton;




- (void)share:(id)sender
    UIButton*btn = (UIButton*)sender;
    if (btn.isSelected)
    
        [btn setSelected:NO];
        [btn setBackgroundImage:[UIImage imageNamed:@"share-icon-deselected-16x16.png"] forState:UIControlStateNormal];
    
    else
    
        [btn setSelected:YES];
        [btn setBackgroundImage:[UIImage imageNamed:@"share-icon-selected-16x16.png"] forState:UIControlStateNormal];
    
;

【讨论】:

我喜欢您使用图像宽度和高度的建议。但请允许我提出一些建议。设置按钮时,使用 [someButton setBackgroundImage:selectedImage forState:UIControlStateSelected] 行设置选定状态的图像; .在您的共享方法中,使用 UIButton 作为参数的类型,并使用以下行进行切换: button.selected = !button.selected;保重。 是的,没错 :) 感谢您的建议@cocoanut。

以上是关于自定义“按下” UIBarButtonItem 背景的主要内容,如果未能解决你的问题,请参考以下文章

在导航栏中自定义 UIBarButtonItem

UIBarButtonItem 和界面旋转的自定义

自定义UIBarButtonItem

添加 UIStackView 作为 UIBarButtonItem 的自定义视图

如何在Xcode + Swift 4中创建自定义UIBarButtonItem类?

UIbarbuttonItem 标题的自定义字体