首页 > 代码库 > ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案
ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的两种解决方案
方案一:利用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:nilaction: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:_actionforControlEvents: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:_actionforControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[UIBarButtonItemalloc]initWithCustomView:button] ;
return item;
}