我正在编写自定义UINavigationBar。由于应用程序应该在iOS 4上运行,因此无法使用new customization options available on iOS 5。
那就是说,我已经将我的UINavigationBar子类化,以避免类别工作得非常好,如下图所示。
UIBarButtonItems也不起作用。
我尝试过继承UIBarButtonItem类并重写drawRect方法,但没有任何改变。
CustomUIBarButtonItem *backButton = [[CustomUIBarButtonItem alloc] initWithTitle:@"Notícias" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
然后我也尝试设置下面的图像,但这也没有用。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"noticias_back.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
那么,我该怎么做呢?
答案 0 :(得分:1)
我只是做了......
UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:@"search_pressed"] forState:UIControlStateHighlighted];
[searchButton addTarget:self action:@selector(selectedSearch) forControlEvents:UIControlEventTouchUpInside];
[searchButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
[searchButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
答案 1 :(得分:0)
您必须先设置自定义按钮,然后再按模式显示。例如:
ENPurchaseEditView *purchaseView = [[ENPurchaseEditView alloc] initWithNibName:@"ENPurchaseEditView" bundle:nil];
// customized left button to cancel - not back.
ENBarButtonImageItem *cancelBtn =
[[ENBarButtonImageItem alloc] initWithFrame:CGRectMake(0, 0, 29, 29)
image:[UIImage imageNamed:@"btn_cancel.png"]
backgroundImage:nil
target:purchaseView
action:@selector(cancel:)];
// setLeftBarButtonItem before you push or present.
// in this view, I also setHidesBackButton = YES in the init so my custom left
// shows up.
[[purchaseView navigationItem] setLeftBarButtonItem:cancelBtn];
[cancelBtn release];
// Edit purchase in full modal view.
// This could be a push - in my case I'm presenting a navController.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:purchaseView];
[[self navigationController] presentModalViewController:navController animated:YES];
在我的例子中,ENBarButtonImageItem是一个自定义的UIBarButtonItem。这是我的班级:
#import "ENBarButtonImageItem.h"
@implementation ENBarButtonImageItem
- (id)initWithFrame:(CGRect)frame
image:(UIImage*)image
backgroundImage:(UIImage*)bgImage
{
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:frame];
self = [super initWithCustomView:_button];
if (self)
{
if (image)
[_button setImage:image forState:UIControlStateNormal];
if (bgImage)
[_button setBackgroundImage:bgImage forState:UIControlStateNormal];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
image:(UIImage*)image
backgroundImage:(UIImage*)bgImage
target:(id)target
action:(SEL)selector
{
self = [self initWithFrame:frame image:image backgroundImage:bgImage];
if (self)
{
[_button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)dealloc
{
[super dealloc];
[_button release];
}
- (void)addTarget:(id)target action:(SEL)selector forControlEvents:(UIControlEvents)controlEvents
{
[_button addTarget:target action:selector forControlEvents:controlEvents];
}
- (void)setImage:(UIImage *)image forState:(UIControlState)state
{
[_button setImage:image forState:state];
}
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
{
[_button setImage:image forState:state];
}
@end