ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案

时间:2014-05-01 17:29:39   收藏:0   阅读:525


demo下载地址:http://pan.baidu.com/s/1c0eROkS


方案一:利用UINavigationBar 自带的布局item(Fixed space Bar Button Item)来适配ios7. 其实就是在原来返回按钮的位置上在多加一个占位的Item


为UINavigationBar 写一个分类。UINavigationItem+correct_offset.h

#import <UIKit/UIKit.h>

@interface UINavigationItem (correct_offset)

- (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem;

- (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem;

@end



#import "UINavigationItem+correct_offset.h"


#define  ios7 ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?YES:NO)

@implementation UINavigationItem (correct_offset)

- (void)addLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem

{

    if (ios7) {

        // Add a spacer on when running lower than iOS 7.0

        UIBarButtonItem *negativeSpacer = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                                                                       target:nil action:nil];

        negativeSpacer.width = -20;

        [self setLeftBarButtonItems:[NSArrayarrayWithObjects:negativeSpacer, leftBarButtonItem,nil]];

    } else {

        // Just set the UIBarButtonItem as you would normally

        [self setLeftBarButtonItem:leftBarButtonItem];

    }

}


- (void)addRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem

{

    if (ios7) {

        // Add a spacer on when running lower than iOS 7.0

        UIBarButtonItem *negativeSpacer = [[UIBarButtonItemalloc]

                                           initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                           target:nilaction:nil];

        negativeSpacer.width = 10;

        [self setRightBarButtonItems:[NSArrayarrayWithObjects:negativeSpacer, rightBarButtonItem,nil]];

    } else {

        // Just set the UIBarButtonItem as you would normally

        [self setRightBarButtonItem:rightBarButtonItem];

    }

}

@end


 在要设置返回按钮的UIViewController中按照如下使用。

[self.navigationItemaddLeftBarButtonItem:[self creatBarItemWithAction:@selector(dismiss)]];

creatBarItemWithAction是我自己写的一个方法。


/**

 *  退出视图。

 */

-(void)dismiss

{

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


/**

 *  创建一个UIBarButtonItem

 *

 *  @param _action action

 *

 *  @return UIBarButtonItem

 */

-(UIBarButtonItem*)creatBarItemWithAction:(SEL)_action{

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [button setImage:[UIImageimageNamed:@"backButton.png"]forState:UIControlStateNormal];

    [button setFrame:CGRectMake(0,0,40, 40)];

    [button addTarget:selfaction:_action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* item = [[UIBarButtonItemalloc] initWithCustomView:button] ;

    return item;

}




方式二:在创建自定义UIBarButtonItem的时候通过设置自定义view的图片偏移属性来做适配。    


 在要设置返回按钮的UIViewController中按照如下使用。

self.navigationItem.leftBarButtonItem = [selfcreatBarItemWithAction:@selector(dismiss)solutiontwo:2];


-(UIBarButtonItem*)creatBarItemWithAction:(SEL)_action solutiontwo:(NSInteger)index{

    UIButton* button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    [button setImage:[UIImageimageNamed:@"backButton.png"]forState:UIControlStateNormal];

    [button setFrame:CGRectMake(0,0,40, 40)];

    if (ios7) {

        [button  setImageEdgeInsets:UIEdgeInsetsMake(0, -30,0, 0)];

    }

    else

    {

        [button  setImageEdgeInsets:UIEdgeInsetsMake(0,0, 0,0)];

    }


    [button addTarget:selfaction:_action forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* item = [[UIBarButtonItemalloc] initWithCustomView:button] ;

    return item;

}






ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!