首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rightbarbuttonitem不显示

rightbarbuttonitem不显示
EN

Stack Overflow用户
提问于 2011-06-23 22:23:41
回答 5查看 4.4K关注 0票数 1

我有下面的代码来放置rightbarbuttonitem

代码语言:javascript
复制
UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightbutton] autorelease];

但是它没有显示任何barbuttonitem。相反,如果我使用下面的代码,那么barbutton项就会出现,但问题是我不能用这个代码在barbuttonitem上设置触摸事件。

代码语言:javascript
复制
UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]];
    UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView];
    self.navigationItem.leftBarButtonItem=icon;
    [icon release];
    [iconView release];
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-06-23 22:34:46

您是否已尝试为rightbutton设置适当的帧?例如rightbutton.frame = (CGRect){CGPointZero, image.size};

另请注意:

在iOS 4和更高版本上,指定文件扩展名时不需要文件名。在iOS 4之前,必须指定文件扩展名。

票数 1
EN

Stack Overflow用户

发布于 2012-02-10 05:05:39

我的猜测是,您将UIBarButtonItem添加到了错误的对象!您需要将其添加到rootViewController (而不是像您可能已经做的那样添加到UINavigationController )

代码语言:javascript
复制
YourRootViewController *theRootController = [[YourRootViewController alloc] init];

UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];

UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
 theRootController.navigationItem.rightBarButtonItem = rightbutton;

[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
票数 7
EN

Stack Overflow用户

发布于 2015-03-31 03:30:01

如果你使用的是白色导航栏,别忘了在UIBarButtonItem上设置tintColor。我的纽扣就在那里,但是看不见。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6455633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档