这是我的代码。这是添加图像,但图像正在导航栏中显示。
UIToolbar* toolbartop = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttonstop = [[NSMutableArray alloc] initWithCapacity:5];
UIBarButtonItem *remainderButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:@"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(LoadOption:)];
[buttonstop addObject:remainderButton];
[remainderButton release];
UIBarButtonItem *faveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(homeAction)];
[buttonstop addObject:faveButton];
[faveButton release];
// put the buttons in the toolbar and release them
[toolbartop setItems:buttonstop animated:NO];
[buttonstop release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbartop];
但是此代码在导航栏中显示图像。
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
为什么左侧导航栏按钮没有显示图像?
[toolbartop release];
答案 0 :(得分:1)
您可以将图像添加到左侧BarButtonItem,如下所示:
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: @"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:@selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(105,0,105,30);
[button2 setBackgroundImage:[UIImage imageNamed: @"image2.png"] forState:UIControlStateNormal];
[button2 addTarget:appDelegate action:@selector(Open_Link2) forControlEvents:UIControlEventTouchUpInside];
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
[button1 release];
[button2 release];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
请确保代码中必须存在“Open_Link1”和“Open_Link2”方法,如下所示:
-(void)Open_Link1
{
// Write your logic
}
-(void)Open_Link2
{
// Write your logic
}
如果您需要更多帮助,请与我们联系。
答案 1 :(得分:0)
框架会自动使用左侧栏按钮项目来显示“后退”按钮。你不能在这里放任何东西,至少在你的导航栏是由UINavigationController管理的时候。为此,您必须自己处理UINavigationBar。
编辑: 在你的leftBarButtonItem中,尝试为工具栏添加UISegmentedControl而不是UIToolbar。
答案 2 :(得分:0)
您正在向工具栏添加按钮,但不是将工具栏添加到任何视图。尝试使用
直接设置按钮self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];