0

By default the back button uses as a text on it a title of a viewcontroller. Can I change text on the back button without changing a title of a view controller? I need this because I have a view controller which title is too long to display and in this case I would like to display just "Back" as a caption for back button.

I tried the following which didn't work:

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

Thanks.

4

2 回答 2

0

尝试使用此代码

UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:@"NewTitle"
                                                         style:UIBarButtonItemStylePlain
                                                        target:[self navigationController]
                                                        action:@selector(popViewControllerAnimated:)
于 2013-10-24T05:16:21.440 回答
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];
于 2013-10-24T05:11:14.947 回答