UINavigationItem 位置问题

解决ios7 UINavigationItem 位置偏移问题

#import <UIKit/UIKit.h>
@interface UINavigationItem (Spacing)

-(void)mySetLeftBarButtonItem:(UIBarButtonItem*)barButton;
-(void)mySetRigthBarButtonItem:(UIBarButtonItem*)barButton;

@end




#import "UINavigationItem+Spacing.h"
#import <objc/runtime.h>

#import <objc/message.h>

@implementation UINavigationItem (Spacing)

// load 在初始化类时调用,每个类都有一个load 方法,

// 类的初始化先于对象

+(void)load

{
   
    //以下方法告诉系统用后面的方法替换前面的
   
    method_exchangeImplementations(
                                  
                                   class_getClassMethod(self, @selector(setLeftBarButtonItem:)),
                                  
                                   class_getClassMethod(self, @selector(mySetLeftBarButtonItem:)));
   
}

- (UIBarButtonItem *)spacer:(float)width

{
   
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                             
                                                                           target:nil action:nil];
   
    if (ios7) {
        space.width = width;
    }
   
    return space ;
   
}

-(void)mySetLeftBarButtonItem:(UIBarButtonItem*)barButton{
    NSArray* barButtons = nil;
   
    barButtons = [NSArray arrayWithObjects: [self spacer:-10.0f], barButton,nil ];
   
    [self setLeftBarButtonItems: barButtons];
   
}

-(void)mySetRigthBarButtonItem:(UIBarButtonItem *)barButton{
    NSArray* barButtons = nil;
   
    barButtons = [NSArray arrayWithObjects: [self spacer:-15.0f], barButton,nil ];
   
    [self setRightBarButtonItems: barButtons];
}

@end

你可能感兴趣的:(UI)