ios拓展9-自定义导航返回按钮

在自定义的nav里,重写initialize;

//========= 系统第一次初始化这个类的时候调用这个方法
//========= 而且只调用一次,比它更早的是load方法
+ (void)initialize{

    
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    
    UIImage *image1 = [UIImage imageNamed:@"home_nav_button_back"];
    
    /*=========下面这句话是重点==========*/
    image1 = [image1 resizableImageWithCapInsets:UIEdgeInsetsMake(0, image1.size.width, 0, 0)];
//其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,
//这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。

    [appearance setBackButtonBackgroundImage:image1
                                    forState:UIControlStateNormal
                                  barMetrics:UIBarMetricsDefault];
//======注意是setBackButtonBackground=======
    [appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    
    NSDictionary *dict = @{
                           NSForegroundColorAttributeName:[UIColor clearColor]
                           };
    
    [appearance setTitleTextAttributes:dict forState:UIControlStateNormal];

}



箭头保持原状

你可能感兴趣的:(ios拓展9-自定义导航返回按钮)