无法更改后退栏按钮文字

时间:2013-10-24 05:08:59

标签: ios objective-c xcode uinavigationitem

默认情况下,后退按钮用作视图控件标题的文本。我可以在不更改视图控制器标题的情况下更改后退按钮上的文本吗?我需要这个,因为我有一个视图控制器,标题太长而无法显示,在这种情况下,我想只显示“后退”作为后退按钮的标题。

我尝试了以下无效的方法:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

感谢。

2 个答案:

答案 0 :(得分:0)

尝试使用此代码:

    UIButton *aCustomBackButton = [UIButton buttonWithType:101]; 
    [aCustomBackButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [aCustomBackButton setTitle:@"customBack" forState:UIControlStateNormal];
    UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
    self.navigationItem.leftBarButtonItem = aLeftBarButtonItem;

对于iOS 7,请使用以下代码:

UIButton *aCustomBackButton = [UIButton buttonWithType:101];
[aCustomBackButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[aCustomBackButton setTitle:@"customBack" forState:UIControlStateNormal];

UIImage *backArrow = [UIImage imageNamed:@"backArrow"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backArrow];
[imageView setFrame:CGRectMake(kScreenOrigin, topMargin, backArrow.size.width, backArrow.size.height)];
[aCustomBackButton addSubview:imageView];

UIBarButtonItem *aNegativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
aNegativeSpacer.width = -8.0;
UIBarButtonItem *aLeftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aCustomBackButton];
self.navigationItem.leftBarButtonItems = @[aNegativeSpacer, aLeftBarButtonItem];

答案 1 :(得分:0)

尝试使用此代码

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
                                                         style:UIBarButtonItemStylePlain
                                                        target:[self navigationController]
                                                        action:@selector(popViewControllerAnimated:)