1

我有这个创建 UIBarButtonItem 的代码:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                               initWithTitle: @"ToDo"
                               style: UIBarButtonItemStyleBordered
                               target:self action: @selector(popToRoot:)];
[self.navigationItem setLeftBarButtonItem:backButton];

问题是删除了左箭头,我想将它包含在文本旁边。我怎样才能做到这一点?

谢谢,

彼得

4

1 回答 1

2

试试这个代码

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];  
UIImage *backBtnImage = [UIImage imageNamed:@"BackBtn.png"]  ;  
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];  
[backBtn addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];  
backBtn.frame = CGRectMake(0, 0, 54, 30);  
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;  
self.navigationItem.leftBarButtonItem = backButton;

然后像这样定义 goback 方法

- (void)goback
{
    [self.navigationController popViewControllerAnimated:YES];
}

注意:BackBtn.png 将包含 ToDo 文本和后退按钮图像...

另一种方式

如果您想不使用图像,请添加以下代码...

-(void)viewWillAppear {
    self.title = @"Your Title"
}

-(void)viewWillDissapear {
    self.title = @"ToDo"
}

当您转到下一个视图时,这将创建 ToDo...

于 2014-02-27T21:59:34.550 回答