本文介绍了如何以编程方式添加BarButton与图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以编程方式创建条形按钮.但是它不能固定在屏幕上.帮助我解决这个问题.

I am creating a barbutton programmatically. But it can't fix into screen. Help me in solve this problem.

屏幕截图:

UIImage *image = [UIImage imageNamed:@"request.png"];
UIBarButtonItem *button2;

//[button2 setWidth:55];
button2= [[UIBarButtonItem alloc] initWithImage:image style:UIBarStyleDefault target:self action:@selector(requestButton)];

self.navigationItem.rightBarButtonItem = button2;

[button2 release];

推荐答案

UIImage *image = [UIImage imageNamed:@"request.png"];    
UIButton* requestButton = [UIButton buttonWithType:UIButtonTypeCustom];
[requestButton setImage:image forState:UIControlStateNormal];
[requestButton addTarget:self action:@selector(requestButton) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:requestButton];
self.navigationItem.rightBarButtonItem = button2;

这篇关于如何以编程方式添加BarButton与图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 17:40