在导航项中隐藏返回文本

Posted

技术标签:

【中文标题】在导航项中隐藏返回文本【英文标题】:Hide Back text in navigation item 【发布时间】:2013-11-26 12:10:20 【问题描述】:

我需要通过替换导航栏中的后退文本(按钮)来设置后退箭头。我可以查看后退箭头,但我不知道如何在导航项中隐藏后退文本。这是我的代码:

因此收到警告: : CGImageCreateWithImageProvider: invalid image size: 0 x 0.

(void)viewWillAppear:(BOOL)animated
 
    UIImage *backButtonHomeImage = [[UIImage imageNamed:@"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    self.navigationItem.leftBarButtonItem = nil;

【问题讨论】:

【参考方案1】:

答案

    -(void)viewWillAppear:(BOOL)animated
     
        [super viewWillAppear:animated];
        if([self.navigationController.viewControllers objectAtIndex:0] != self)
        
            UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
            [backButton setImage:[UIImage imageNamed:@"back-arrow.png"] forState:UIControlStateNormal];
            [backButton setShowsTouchWhenHighlighted:TRUE];
            [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
            UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
            self.navigationItem.hidesBackButton = TRUE;
            self.navigationItem.leftBarButtonItem = barBackItem;
        


-(void)popViewControllerWithAnimation
  
   [self.navigationController popViewControllerAnimated:YES];


可能会有所帮助。

【讨论】:

非常适合我,但如果我想对右栏按钮做同样的事情怎么办。【参考方案2】:

试试这个:

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];

【讨论】:

【参考方案3】:

创建自定义后退按钮并将该按钮添加到导航控制器的后退按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0,0,54,32);
UIImage *backButtonImage = [UIImage imageNamed:@"back-arrow.png"];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];

【讨论】:

【参考方案4】:

在推送到该视图控制器之前,只需将空白文本设置为导航栏的标题,如下所示。

self.navigationItem.title=@" ";

【讨论】:

Smartwork:它现在正在工作,但箭头显示在中间。需要在角落里展示吗? Smartwork:如果我扩展空间,它会显示点。我陷害了 UIImage *backButtonHomeImage = [[UIImage imageNamed:@"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];【参考方案5】:

我希望这会奏效

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

【讨论】:

以上是关于在导航项中隐藏返回文本的主要内容,如果未能解决你的问题,请参考以下文章

ios隐藏左侧导航按钮的文本

从片段导航到另一个片段时隐藏键盘

19.ToolBar实现隐藏,返回,搜索,三个点强制显示

为啥我的 UITextView 文本从开始时不显示并隐藏在导航栏下

如何在 SwiftUI 中动态隐藏导航返回按钮

检测 UISearchController 何时出现在 iOS 11 的导航项中