如何在UINavigationBar的rightBarButtonItem上设置图像?

Ash*_*osh 8 iphone objective-c uinavigationbar

我试图在rightBarButtonItem上设置图像并且它很好,但唯一不对的是这个图像背后的背景,它的宽度比我的图像宽.任何人都知道如何解决这个问题.码:

[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"action_btn.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(actionButtonClicked:)] animated:YES];
Run Code Online (Sandbox Code Playgroud)

App*_*ect 19

在这里,您可以在NavigationBar的rightBarButtonItem上设置图像,如下所示:

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

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button1];
[button1 release];
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请与我们联系.