8

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];
4

4 に答える 4

24

配色に一致するさまざまな制御状態の背景画像custom UIButtonを使用して作成し、これをのビューとして使用する必要があると思います。UIButtoncustom UIBarButtonItem

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

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

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

他のリファレンスが必要な場合は、この美しいリンクをたどることができます:外観プロキシを使用したアプリのスタイル設定

于 2013-01-19T11:04:57.970 に答える
3

あなたはおそらくこのように試すことができます。私のために働きます。

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];
于 2013-01-19T19:38:06.527 に答える
2

多分これはあなたを助けるでしょう(プロジェクトにそのファイルを追加するだけです):

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];
}
于 2013-01-23T00:13:08.413 に答える
1
    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];
    }
};
于 2014-08-10T23:33:03.050 に答える