我有下面的代码来放置rightbarbuttonitem
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上设置触摸事件。
UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]];
UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView];
self.navigationItem.leftBarButtonItem=icon;
[icon release];
[iconView release];发布于 2011-06-23 22:34:46
您是否已尝试为rightbutton设置适当的帧?例如rightbutton.frame = (CGRect){CGPointZero, image.size};
另请注意:
在iOS 4和更高版本上,指定文件扩展名时不需要文件名。在iOS 4之前,必须指定文件扩展名。
发布于 2012-02-10 05:05:39
我的猜测是,您将UIBarButtonItem添加到了错误的对象!您需要将其添加到rootViewController (而不是像您可能已经做的那样添加到UINavigationController )
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];发布于 2015-03-31 03:30:01
如果你使用的是白色导航栏,别忘了在UIBarButtonItem上设置tintColor。我的纽扣就在那里,但是看不见。
https://stackoverflow.com/questions/6455633
复制相似问题