【问题标题】:Can't change back bar button text无法更改后退栏按钮文本
【发布时间】:2013-10-24 05:08:59
【问题描述】:

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

我尝试了以下方法,但没有成功:

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

谢谢。

【问题讨论】:

  • 问题出在你的目标上..
  • 目标集应该是 self 而不是 nil

标签: ios objective-c xcode uinavigationitem


【解决方案1】:

试试这个代码

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

【讨论】:

    【解决方案2】:

    试试这个代码:

        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];
    

    【讨论】:

    • @user2885928 上面的代码有什么问题,请不要使用单词的排序形式这不是聊天室,请使用正确的拼写。什么是指针?
    • "
    • 我猜你正在使用 iOS 7。请查看我的更新回复。
    • 我认为没有必要为它设置自定义按钮@Abhinav
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2019-01-16
    • 1970-01-01
    相关资源
    最近更新 更多