如何删除leftBarButtonItem蓝色背景?
我试过style:UIBarButtonItemStylePlain
但没有结果。
代码:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_home"] style:UIBarButtonItemStylePlain target:self.navigationController action:@selector(openMenu) ];
答案 0 :(得分: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]];
答案 1 :(得分:1)
在你的案例中UINavigationController
在管理导航栏和其中的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
设置为self
而不是self.navigationController
。
答案 2 :(得分: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,你甚至不需要对个别实例大惊小怪。
答案 3 :(得分: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]];
这将工作请尝试....