问题描述
- 自定义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文本。
解决方案
试试我提供的方法
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""Recent"" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRecent)]; self.navigationItem.leftBarButtonItem = backButtonItem;
解决方案二:
应该在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];
时间: 2024-12-22 09:08:42