set custom back bar button universally with no title

前端 未结 6 1286
暖寄归人
暖寄归人 2021-01-03 02:36

I want to set custom back bar button for all controllers in the app. I tried using this:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backB

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 03:22

    You could write a category like this,

    //
    //  UINavigationItem+BarAditions.h
    //
    //  Created by Satheeshwaran on on 7/5/13.
    //
    
    
    #import 
    
    
    @interface UINavigationItem (PersonaAddition)
    
    - (void)setCustomBackButton;
    
    @end
    
    
    //
    //  UINavigationItem+BarAditions.m
    //
    //  Created by Satheeshwaran on on 7/5/13.
    //
    
    #import "UINavigationItem+PersonaAddition.h"
    
    
    @implementation UINavigationItem (PersonaAddition)
    
    - (void)setCustomBackButton 
    {
        //customize ur back button here.
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame=CGRectMake(0, 0, 60, 30);
        [backButton addTarget:target action:@selector(didPressLeftItem:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.leftBarButtonItem = barItem;    
    
    }
    

    and import this category in all ur files and call the setCustomBackButton in all ur classes. This works fine for me, even in iOS 7.

    In ViewDidLoad of all ur classes.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [self.navigationItem setCustomBackButton];
    
    }
    

提交回复
热议问题