美文网首页
iOS 运行时隐藏导航栏返回按钮文字

iOS 运行时隐藏导航栏返回按钮文字

作者: TeacherXue | 来源:发表于2019-06-19 17:32 被阅读0次

    给UIViewController添加分类,在分类中添加以下方法

    +(void)load {
      swizzleMethod([self class], @selector(viewDidAppear:), @selector(ac_viewDidAppear));
    }
    - (void)ac_viewDidAppear{
      self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                           initWithTitle:@""
                           style:UIBarButtonItemStylePlain
                           target:self
                           action:nil];
      [self ac_viewDidAppear];
    }
    void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector)
    {
      // the method might not exist in the class, but in its superclass
      Method originalMethod = class_getInstanceMethod(class, originalSelector);
      Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
      // class_addMethod will fail if original method already exists
      BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
      // the method doesn't exist and we just added one
      if (didAddMethod) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
      }
      else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
      }
    }
    

    相关文章

      网友评论

          本文标题:iOS 运行时隐藏导航栏返回按钮文字

          本文链接:https://www.haomeiwen.com/subject/vytlqctx.html