2 ooomiao oOOMIAO 于 2013.04.01 10:49 提问

自定义UIBarButtonItem的文本

需要给UIBarButtonItem设置标题

代码如下:

UIButton* backToRecent = [UIButton buttonWithType:UIButtonTypeCustom];
[backToRecent setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];

[backToRecent addTarget:self action:@selector(backToRecent) forControlEvents:UIControlEventTouchUpInside];
[backToRecent setFrame:CGRectMake(0, 0, 60, 30)];

UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backToRecent];
[backButtonItem setTitle:@"Recent"];
partViewController.navigationItem.leftBarButtonItem = backButtonItem;
[backButtonItem release];

但是按钮显示后,没有recent文本。

2个回答

vikeyToy
vikeyToy   2013.04.01 11:03

试试我提供的方法

   UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Recent" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRecent)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
O_1001_O
O_1001_O   2013.04.01 15:06

应该在UIButton设置标题 , 不是在UIBarButtonItem

[backToRecent setTitle:@"Recent" forState:UIControlStateNormal];

而且不鼓励让变量和方法同名

[backToRecent addTarget:self action:@selector(backToRecent) forControlEvents:UIControlEventTouchUpInside];

这个不允许是因为你使用了:

[backToRecent setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];

换成用用这个试试:

[backToRecent setBackgroundImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!