ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正
为UINavigationBar 写一个分类。UINavigationItem+correct_offset.h  
转载 http://www.colabug.com/thread-1112420-1-1.html
  #import  
  @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 = [[ UIBarButtonItem alloc ] initWithBarButtonSystemItem : UIBarButtonSystemItemFixedSpace 
                                                                                          target : nil action : nil ]; 
          negativeSpacer. width = - 20 ; 
          [ self setLeftBarButtonItems :[ NSArray arrayWithObjects :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 = [[ UIBarButtonItem alloc ] 
                                             initWithBarButtonSystemItem : UIBarButtonSystemItemFixedSpace 
                                             target : nil action : nil ]; 
          negativeSpacer. width = 10 ; 
          [ self setRightBarButtonItems :[ NSArray arrayWithObjects :negativeSpacer, rightBarButtonItem, nil ]]; 
      } else { 
          // Just set the UIBarButtonItem as you would normally 
          [ self setRightBarButtonItem :rightBarButtonItem]; 
     }
}
@end
  在要设置返回按钮的UIViewController中按照如下使用。
  [ self . navigationItem addLeftBarButtonItem: [ self  creatBarItemWithAction : @selector (dismiss)]]; 
   creatBarItemWithAction是我自己写的一个方法。 
  
/**
   *  退出视图。 
  */
  -( void )dismiss 
{
      [ self dismissViewControllerAnimated : YES completion : nil ]; 
}
/**
   *  创建一个 UIBarButtonItem 
  *
  *  @param _action action
  *
  *  @return UIBarButtonItem
  */
  -( UIBarButtonItem *)creatBarItemWithAction:( SEL )_action{ 
      UIButton * button = [ UIButton buttonWithType : UIButtonTypeCustom ]; 
      [button setImage :[ UIImage imageNamed : @"backButton.png" ] forState : UIControlStateNormal ]; 
      [button setFrame : CGRectMake ( 0 , 0 , 40 , 40 )]; 
      [button addTarget : self action :_action forControlEvents : UIControlEventTouchUpInside ]; 
      UIBarButtonItem * item = [[ UIBarButtonItem alloc ] initWithCustomView :button] ; 
      return item; 
}
   方式二:在创建自定义 UIBarButtonItem 的时候通过设置自定义view的图片偏移属性来做适配。      
   在要设置返回按钮的UIViewController中按照如下使用。
self . navigationItem . leftBarButtonItem = [ self creatBarItemWithAction : @selector (dismiss) solutiontwo : 2 ]; 
  -( UIBarButtonItem *)creatBarItemWithAction:( SEL )_action solutiontwo:( NSInteger )index{ 
      UIButton * button = [ UIButton buttonWithType : UIButtonTypeCustom ]; 
      [button setImage :[ UIImage imageNamed : @"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 : self action :_action forControlEvents : UIControlEventTouchUpInside ]; 
      UIBarButtonItem * item = [[ UIBarButtonItem alloc ] initWithCustomView :button] ; 
      return item; 
}
ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正的更多相关文章
- IOS7 自定义UIBarButtonItem 的一些问题
		ios 下自定义导航栏的BarButtonItem 会产生一些偏移问题, 解决方案: 通过新建一个系统的带固定距离的Item来调节你的Item #define IOS7_NAVI_SPACE ... 
- iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮
		iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ... 
- iOS7自定义back按钮和pop交互手势
		Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ... 
- IOS中GPS定位偏移纠正(适用于Google地图)
		在这个神奇的国度里,我们总得学习一些有中国特色的东东,例如“火星坐标”.也许有人还不知道这是什么玩意,我就简要介绍一下吧. 如果你有带GPS模块的智能手机,打开定位功能,然后访问Google ... 
- IOS7学习之路九(ios7自定义UIAlertView)
		IOS7的UIAlertView 不支持自定义,无法添加subview . 不过可以用第三方库git上的下载链接 https://github.com/wimagguc/ios-custom-a ... 
- 自定义UINavigationBar的背景【转】
		from:http://cocoa.venj.me/blog/custom-navbar-background/ 为了让我们的应用程序更加美观,我们往往希望对iPhone自带的控件进行一点自定义.比如 ... 
- ios之自定义UINavigationBar
		ios5 自定义导航条问题 在ios5之前的系统中,可以通过定义导航条类别的方式自定义导航条: @implementation UINavigationBar (CustomImage)- (void ... 
- iOS7自定义statusbar和navigationbar的若干问题
		当然有许多问题是这篇文章中没有提到的,按照文章的方法进行设置,你可能会遇到以下问题: 1.navigationbar的背景图片自定义以后,statusbar虽然和navigationbar共用了背景图 ... 
- iOS LBS相关: 定位和中国特色的位置偏移纠正
		LBS模块,首先当然是定位,获取自己所在的位置.主要用到的就是CLLocationManager,实例一个,然后调用startUpdatingLocation即可.其中可以指定精度CLLocation ... 
随机推荐
- PHP5中常用的魔术函数有哪些,举例说明各自的用法。
			所谓魔术函数,是指不同时刻为了完成特定的功能,而自动调用的方法. __sleep 在serialize()序列化对象之前会调用这个函数__wakeup 在unserialize()解 ... 
- paypal api 相关资料
			https://developer.paypal.com/ https://developer.paypal.com/docs/classic/api/merchant/GetBalance_API_ ... 
- 03-树2 List Leaves
			二叉树及其遍历 一遍AC,挺开心的hhh~ 简单讲下思路:叶子,顾名思义就是没有左右子树的结点.由于题目要求,叶子结点的输出顺序是从上往下,从左往右.所以用层序遍历法. 当然,这里先找到root树的根 ... 
- WPF控件数据单项绑定
			建立一个姓名,年龄输入框,如图: XAML代码: <TextBox Name="txtName" Text="{Binding Name}" ToolTi ... 
- 与谷歌测试工程师的对话 - from Google Testing Blog
			Conversation with a Test Engineer by Alan Faulner Alan Faulner谷歌的一名测试工程师,他工作在DoubleClick Bid Manager ... 
- html/css  盒子布局 Margin 、Padding 、border 以及 清除浮动的知识 (学习HTML过程中的小记录)
			html/css 盒子布局 Margin .Padding .border 以及 清除浮动的知识 (学习HTML过程中的小记录) 作者:王可利(Star·星星) width 是"宽 ... 
- python入门总结-函数
			函数形式: def functionname(paramlist): function body 局部变量不改变实参的值,如果需要改变,声明global.比如,global x 可以给函数默认值,注意 ... 
- EXCLE中快速插入图片
			在excle中怎么快速插入图片呢,一张一张点实在比较麻烦 解决办法: <table><img src="D:\A.png" width="60" ... 
- JavaScript高级程序设计之作用域链
			JavaScript只有函数作用域:每个函数都有个作用域链直达window对象. 变量的查找由内而外层层查找,找到即止. 同时不仅可以查找使用,甚至可以改变外部变量. var color = &quo ... 
- "奇葩家园“之 asyncTask 与 url 下载篇
			asyncTask 是android提供的一个轻量级的异步处理的类,有3个泛型参数,params,progress,result params: 启动任务执行的时候传入的参数比如请求的 url 地址 ... 
