为什么导航栏的 UIBarButton 项目没有图像

发布于 2024-11-19 18:29:09 字数 1956 浏览 2 评论 0原文

这是我的代码。这是添加图像,但图像出现在导航栏中。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瞳孔里扚悲伤 2024-11-26 18:29:09

您可以将图像添加到左侧 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
}

如果您需要更多帮助,请告诉我。

You can add the image to your left BarButtonItem as follows:

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];

Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:

-(void)Open_Link1
{
     // Write your logic
}

-(void)Open_Link2
{
     // Write your logic
}

Let me know if you want more help.

碍人泪离人颜 2024-11-26 18:29:09

框架自动使用左栏按钮项来显示“后退”按钮。您不能在此处放置任何内容,至少当您的导航栏由 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.

不再让梦枯萎 2024-11-26 18:29:09

您正在向工具栏添加按钮,但没有将工具栏添加到任何视图。尝试直接使用设置按钮

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];

You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文