2

如何删除 leftBarButtonItem 蓝色背景?我试过style:UIBarButtonItemStylePlain但没有结果。

代码:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_home"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(openMenu) ];

在此处输入图像描述

4

4 回答 4

8
    UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [tempButton setFrame:CGRectMake(2, 1, 34, 34)]; // your Home Button Image width and height.
    [tempButton addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];
    [tempButton setImage:[UIImage imageNamed:@"icon_home.png"] forState:UIControlStateNormal];
    [tempButton setImage:[UIImage imageNamed:@"icon_home_h.png"] forState:UIControlStateHighlighted];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:tempButton]];
于 2013-04-22T05:03:59.400 回答
1

在您的情况下UINavigationController,在管理它的 navigationBar 和它时,它会做一些额外的样式UIBarButtonItems。因此,将样式设置为 plain 与使用UIToolbar.

您需要的是自定义视图,例如...

UIImage *image = [UIImage imageNamed:@"icon_home"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
barButtonItem.target = self;
barButtonItem.action = @selector(openMenu);
self.navigationItem.leftBarButtonItem = barButtonItem;

此外,您可能希望target设置为selfnot self.navigationController

于 2013-04-19T22:05:22.730 回答
0

如果您只关心 iOS 5+,还有另一种不涉及自定义视图的方法。随着 UIAppearance 添加,您可以使用

UIImage * emptyImage() {
    UIGraphicsBeginImageContext(CGSizeMake(1, 1));
    UIImage * emptyImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return emptyImage;
}

UIBarButtonItem * item = [[UIBarButtonItem alloc] initWith...];
[item setBackgroundImage:emptyImage() forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationItem.leftBarButtonItem = item;

如果这是一个应用程序范围的风格,那就做

[[UIBarButtonItem appearance] setBackgroundImage:emptyImage() forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

一旦在您的 AppDelegate 中,您甚至不需要对单个实例大惊小怪。

于 2013-04-23T02:59:59.340 回答
0
UIButton *btn= [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0,30, 40)];    
[btn setImage:[UIImage imageNamed:@"icon_home.png"] forState:UIControlStateNormal];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:btn]];

这将工作请尝试....

于 2013-04-24T17:07:10.717 回答