毕竟最后还是需要自定义navigationItem

2016-06-20  本文已影响654人  Axiba

果然还是simple,之前为了不自定义navigationItem而纯修改文本颜色搞了一波(http://www.jianshu.com/p/38600293be61)
开始觉得还好,其实仔细一看会发现字体显粗了,大概重新绘制了字体文本导致的,所以还是写个分类什么的,或者,你猜?

1、下面先用分类的方式实现自定义文本导航和图片导航UIBarButtonItem+Extension.h

/**
 *  封装一个图片item公共项
 *
 *  @param target     对应的控制器
 *  @param action     点击事件
 *  @param image      正常下图片
 *  @param hightImage 点击时图片
 *
 *  @return UIBarButtonItem item
 */
+(UIBarButtonItem *)itemWithAction:(id)target action:(SEL)action imageName:(NSString *)image highImageName:(NSString *)hightImage
{
    //自定义一个按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateHighlighted];
    
    btn.size= btn.currentBackgroundImage.size;
    
    return [[UIBarButtonItem alloc]initWithCustomView:btn];
}
/**
 *  封装一个文本item公共项
 *
 *  @param target     对应的控制器
 *  @param action     点击事件
 *  @param title      item文本
 *  @param normalColoc 文本默认颜色
 *
 *  @return UIBarButtonItem item
 */
+(UIBarButtonItem *)itemWithTitleAction:(id)target action:(SEL)action title:(NSString *)title normalColoc:(UIColor *)normalColoc
{
    //自定义一个按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:normalColoc forState:UIControlStateNormal];
    //[btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    CGSize titleSize = [title suggestedSizeWithFont:btn.titleLabel.font];
    btn.frame = (CGRect){{0,0}, titleSize};
    
    return [[UIBarButtonItem alloc]initWithCustomView:btn];
}

2、当然,当我们实现了基本的效果,OK,看起来确实还不错,但是细心的童鞋就会发现,无论在左侧还是右侧,文字或者图标都会相对比系统默认的间距要远离边缘,如果你是洁癖佬或者你的交互设计师视觉设计师是,这时候可能要考虑一下对间距的控制了:

所以我在另一个分类对此做一个控制UINavigationItem+marginOffset.h

- (void)addLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spaceButtonItem.width = -3;
    if (DIOS7_Later) {
        spaceButtonItem.width = -14;
    }
    
    if (_leftBarButtonItem) {
        [self setLeftBarButtonItems:@[spaceButtonItem, _leftBarButtonItem]];
    } else {
        [self setLeftBarButtonItems:@[spaceButtonItem]];
    }
}
- (void)addRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
   
    spaceButtonItem.width = -8;
    if (DIOS7_Later) {
        spaceButtonItem.width = -16;
        
    }
    if (_rightBarButtonItem)
    {
        [self setRightBarButtonItems:@[spaceButtonItem, _rightBarButtonItem]];
    }
    else
    {
        [self setRightBarButtonItems:@[spaceButtonItem]];
    }
}

3、当然如果你觉得你想自定义自己的间距,改变一下参数设置就可以了:

- (void)addLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem marginOffset_iOS6:(CGFloat)marginOffset_iOS6 marginOffset_iOS7orLater:(CGFloat)marginOffset_iOS7orLater
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spaceButtonItem.width = marginOffset_iOS6;
    if (DIOS7_Later) {
        spaceButtonItem.width = marginOffset_iOS7orLater;
    }
    
    if (_leftBarButtonItem) {
        [self setLeftBarButtonItems:@[spaceButtonItem, _leftBarButtonItem]];
    } else {
        [self setLeftBarButtonItems:@[spaceButtonItem]];
    }
}

- (void)addRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem marginOffset_iOS6:(CGFloat)marginOffset_iOS6 marginOffset_iOS7orLater:(CGFloat)marginOffset_iOS7orLater;
{
    UIBarButtonItem *spaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    spaceButtonItem.width = marginOffset_iOS6;
    if (DIOS7_Later) {
        spaceButtonItem.width = marginOffset_iOS7orLater;
        
    }
    
    if (_rightBarButtonItem)
    {
        [self setRightBarButtonItems:@[spaceButtonItem, _rightBarButtonItem]];
    }
    else
    {
        [self setRightBarButtonItems:@[spaceButtonItem]];
    }
}

4、当然事实上,这些间距的问题只会在iOS6+才会出现的,如果你洁癖得还不够过瘾,可以 将上面修改间距的四个方法包在这段代码里面:

if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

endif

5、当然如果从页面结构角度去操作代码,可以考虑在基类里面写一个初始化导航栏的复写方法

- (void)initNavBarLeftBtnWithNormalImage:(NSString *)normalImage highlightedImage:(NSString *)highlightedImage target:(id)target action:(SEL)action
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 44, 44)];
    [leftButton setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self.navigationItem addLeftBarButtonItem:leftButtonItem];
}

- (void)initNavBarLeftBtnWithNormalImage:(NSString *)normalImage highlightedImage:(NSString *)highlightedImage target:(id)target action:(SEL)action marginOffset_iOS6:(CGFloat)marginOffset_iOS6 marginOffset_iOS7orLater:(CGFloat)marginOffset_iOS7orLater
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 44)];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
    [leftButton setImage:[UIImage imageNamed:normalImage] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:highlightedImage] forState:UIControlStateHighlighted];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [self.navigationItem addLeftBarButtonItem:leftButtonItem marginOffset_iOS6:marginOffset_iOS6 marginOffset_iOS7orLater:marginOffset_iOS7orLater];
}
上一篇下一篇

猜你喜欢

热点阅读