【问题标题】:Removing the title text of an iOS UIBarButtonItem删除 iOS UIBarButtonItem 的标题文本
【发布时间】:2023-04-06 06:44:01
【问题描述】:

我想要做的是从UIBarButtonItem 的“返回”按钮中删除文本,只留下导航栏上的蓝色 V 形。请记住,我正在为 iOS 7 进行开发。我尝试了多种方法,包括但不限于:

这是我不喜欢的图像方法(图像看起来不合适):

UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iOS7BackButton"] style:UIBarButtonItemStylePlain target:self action:@selector(goToPrevious:)];
self.navigationItem.leftBarButtonItem = barBtnItem;

我尝试的另一种方法是这样,它根本不起作用(什么都没有显示):

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]init];
barBtn.title=@"";
self.navigationItem.leftBarButtonItem=barBtn;

我想要实现的是类似于 iOS 7 音乐应用中的后退按钮,它只有一个 V 形。

谢谢。

【问题讨论】:

  • 你为什么不拍下你的要求是什么?并在 leftBarButtonItem 中引用它。
  • 我没有使用图片方法的原因是1。很难获得完美的后退按钮图片和2。图像会出现某种形式的错位,它不会看起来不自然,这就是为什么我向 StackOverflow 寻求有关如何在本地完成此操作的帮助。

标签: ios uinavigationbar uibarbuttonitem uinavigationitem


【解决方案1】:

如果你想在iOS11中去掉后退按钮项标题,可以试试!

@implementation UIView (PrivateBackButton)
    
    + (void)initialize {
        if ([NSProcessInfo processInfo].operatingSystemVersion.majorVersion < 11.0) return;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            [NSObject hookInstanceMethodForClass:[self class] originalMethod:@selector(layoutSubviews) newMethod:@selector(mc_layoutSubviews)];
        });
    }
    
    - (void)mc_layoutSubviews {
        [self mc_layoutSubviews];
        [self updateiOS11LaterUI];
    }
    
    - (void)updateiOS11LaterUI {
        if ([NSProcessInfo processInfo].operatingSystemVersion.majorVersion < 11.0) return;
        if (![self isKindOfClass:NSClassFromString(@"_UIBackButtonContainerView")]) return;
        
        [self removeAllSubviews];
        [self.superview mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(@44);
        }];
    }
    
    @end

2021 年更新:使用 NXNavigationExtension

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的短期帮助。一个正确的解释would greatly improve 其长期价值,通过展示为什么这是解决问题的好方法,并将使其对有其他类似问题的未来读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
【解决方案2】:

我无法使用 Guto Araujo 的回答 navigationBar.topItem.title = @""; 来让它工作

但是,通过在我的视图控制器的init 方法中设置self.title = @"",我能够获得所需的效果。 (设置在init 很重要,viewDidLoad 不起作用。)

【讨论】:

    【解决方案3】:

    我列出了迄今为止对我有用的解决方案。

    override func viewDidLoad() {
    super.viewDidLoad()   
    
    
    
    self.navigationController?.navigationBar.topItem?.title = "" // 1
    
     let barAppearace = UIBarButtonItem.appearance()
    barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default)  // 2
    
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal) //3
    
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted) //4   
    
    
    
    }
    

    【讨论】:

      【解决方案4】:

      实现您自己的UIViewController 基类并覆盖setTitle:

      @interface CustomViewController : UIViewController
      @end
      
      @implementation CustomViewController
      
      - (void)setTitle:(NSString *)title {
          super.title = @""; // This will remove the "Back" title
          UILabel *titleView = [UILabel new];
          // customize the label as you wish
          titleView.text = title;
          [titleView sizeToFit];
          self.navigationItem.titleView = titleView;
      }
      
      @end
      

      现在在任何你想设置标题的UIViewController,你可以像往常一样写self.title = @"MyTitle",当推送一个新的UIViewController时,后栏项目不会出现任何文字。

      【讨论】:

        【解决方案5】:

        我制作了一个非常简单的零配置类别来隐藏所有应用程序中的后退按钮标题,您可以查看here。此问题已被接受,但对其他人可能会有所帮助。

        编辑:

        .h 文件

        #import <UIKit/UIKit.h>
        
        @interface UINavigationController (HideBackTitle)
        extern void PJSwizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector);
        
        @end
        

        .m 文件

        #import "UINavigationController+HideBackTitle.h"
        #import <objc/runtime.h>
        
        
        @implementation UINavigationController (HideBackTitle)
        
        + (void)load {
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                PJSwizzleMethod([self class],
                            @selector(pushViewController:animated:),
                            @selector(pj_pushViewController:animated:));
            });
        }
        
        - (void)pj_pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
            UIViewController *disappearingViewController =  self.viewControllers.lastObject;
            if (disappearingViewController) {
                disappearingViewController.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
            }
            if (!disappearingViewController) {
                return [self pj_pushViewController:viewController animated:animated];
            }
            return [self pj_pushViewController:viewController animated:animated];
        }
        
        
        
        @end
        
        void PJSwizzleMethod(Class cls, SEL originalSelector, SEL swizzledSelector)
        {
            Method originalMethod = class_getInstanceMethod(cls, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(cls, swizzledSelector);
        
            BOOL didAddMethod =
            class_addMethod(cls,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        
            if (didAddMethod) {
                class_replaceMethod(cls,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
        

        【讨论】:

          【解决方案6】:

          在 iOS 11 中,您可以使用下一个代码隐藏返回按钮标题:

          斯威夫特:

          UIBarButtonItem.appearance().setTitleTextAttributes([ NSForegroundColorAttributeName : UIColor.clear ], for: .normal)
          UIBarButtonItem.appearance().setTitleTextAttributes([ NSForegroundColorAttributeName : UIColor.clear ], for: .highlighted)
          

          此代码不会从导航栏中删除标题,只是使其透明,后退按钮仍保留标题空间。如果您需要为视图控制器标题留出更多空间,则需要使用其他解决方案。

          【讨论】:

          • 这也使 所有 按钮透明,而不仅仅是后退按钮。
          【解决方案7】:

          只需要在ParentViewController中设置空白标题文本。

          self.navigationItem.title=@"";
          

          如果您需要标题文本,则在 ParentViewController 中放入以下两个方法。

          -(void)viewWillAppear:(BOOL)animated
          {
              self.navigationItem.title = @"TitleText";
          }
          
          -(void)viewWillDisappear:(BOOL)animated
          {
              self.navigationItem.title=@"";
          }
          

          【讨论】:

            【解决方案8】:

            只需设置 UIBarButtonItem 外观的偏移量。

            [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
                                                                 forBarMetrics:UIBarMetricsDefault];
            

            【讨论】:

            • 导航栏标题似乎仍然偏移以为后退按钮文本腾出空间,即使它不可见。
            【解决方案9】:

            这在 iOS 7+ 中对我有用:

            在 viewDidLoad 中:

            self.navigationItem.backBarButtonItem.title = @" ";
            

            是的,这是引号之间的空格。

            【讨论】:

            • 如果从代码中推送视图控制器,则无法正常工作。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-12-30
            • 2017-10-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多