为什么导航栏的 UIBarButton 项目没有图像
这是我的代码。这是添加图像,但图像出现在导航栏中。
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];
Here is my code. This is adding image but images are being appeared in navigation bar.
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];
But this code is showing image in navigation right bar.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
Why left navigation bar button is not showing images?
[toolbartop release];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将图像添加到左侧 BarButtonItem,如下所示:
这里请确保代码中必须存在方法“Open_Link1”和“Open_Link2”,如下所示:
如果您需要更多帮助,请告诉我。
You can add the image to your left BarButtonItem as follows:
Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:
Let me know if you want more help.
框架自动使用左栏按钮项来显示“后退”按钮。您不能在此处放置任何内容,至少当您的导航栏由 UINavigationController 管理时不能。为此,您必须自己处理 UINavigationBar。
编辑:
在 leftBarButtonItem 中,尝试将 UISegmentedControl 而不是 UIToolbar 作为工具栏顶部。
The left bar button item is automatically used by the framework for showing a "Back" button. You can't put anything here, at least not when your navigation bar is managed by a UINavigationController. For this to work you have to handle the UINavigationBar yourself.
Edit:
in your leftBarButtonItem, try putting a UISegmentedControl instead of a UIToolbar for toolbartop.
您正在向工具栏添加按钮,但没有将工具栏添加到任何视图。尝试直接使用设置按钮
You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using