为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的偏移纠正的更多相关文章

  1. IOS7 自定义UIBarButtonItem 的一些问题

    ios 下自定义导航栏的BarButtonItem 会产生一些偏移问题, 解决方案: 通过新建一个系统的带固定距离的Item来调节你的Item #define   IOS7_NAVI_SPACE   ...

  2. iOS开发备忘录:自定义UINavigationBar背景图片和Back按钮

    iOS项目,根据设计图,有时需要自定义UIView的UINavigationBar的背景.可以切出来一张1像素左右的背景图片,来充当UINavigationBar的背景. 可以利用Navigation ...

  3. iOS7自定义back按钮和pop交互手势

    Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ...

  4. IOS中GPS定位偏移纠正(适用于Google地图)

    在这个神奇的国度里,我们总得学习一些有中国特色的东东,例如“火星坐标”.也许有人还不知道这是什么玩意,我就简要介绍一下吧.      如果你有带GPS模块的智能手机,打开定位功能,然后访问Google ...

  5. IOS7学习之路九(ios7自定义UIAlertView)

    IOS7的UIAlertView 不支持自定义,无法添加subview . 不过可以用第三方库git上的下载链接    https://github.com/wimagguc/ios-custom-a ...

  6. 自定义UINavigationBar的背景【转】

    from:http://cocoa.venj.me/blog/custom-navbar-background/ 为了让我们的应用程序更加美观,我们往往希望对iPhone自带的控件进行一点自定义.比如 ...

  7. ios之自定义UINavigationBar

    ios5 自定义导航条问题 在ios5之前的系统中,可以通过定义导航条类别的方式自定义导航条: @implementation UINavigationBar (CustomImage)- (void ...

  8. iOS7自定义statusbar和navigationbar的若干问题

    当然有许多问题是这篇文章中没有提到的,按照文章的方法进行设置,你可能会遇到以下问题: 1.navigationbar的背景图片自定义以后,statusbar虽然和navigationbar共用了背景图 ...

  9. iOS LBS相关: 定位和中国特色的位置偏移纠正

    LBS模块,首先当然是定位,获取自己所在的位置.主要用到的就是CLLocationManager,实例一个,然后调用startUpdatingLocation即可.其中可以指定精度CLLocation ...

随机推荐

  1. 开源web终端ssh解决方案-gateone简介

    好久都没来写博客,最近忙啥去了呢? 一是忙于saltstack的二次开发,二是云计算的学习研究中,所以就一直没写东西,今天给大家介绍个工具. 1. 首先来说一下为什么要 web ssh? 许多人不是说 ...

  2. C# 乘法口诀表的实现方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 乘法运算 ...

  3. python笔记第二天

    上节内容回顾和补充 编程语言 高级 低级 Python种类 JavaPython cPython ***** pypy 字节码 和 机器码 Python程序: 1. 终端: C:\python35\p ...

  4. 5.css字体

    下面的用一个表格总结了文本样式中字体的一些设置方法: 属性名 说明 CSS 版本 font-size 设置字体的大小 1 font-variant 设置英文字体是否转换为小型大写 1 font-sty ...

  5. rails bug

    variable @fontAwesomeEotPath_iefix is undefined rails generate bootstrap:install如果还有错,保证在加载主提之前impor ...

  6. opengl基础学习专题 (三) 多边形绘制的几种样式

    题外话 聪明人之所以不会成功,是由于他们缺乏坚韧的毅力. ——艾萨克·牛顿(1643年1月4日—1727年3月31日)英国 也许可以理解为 想更深一步的时候,坚持,努力和聪明缺一不可. 挺直腰杆在此向 ...

  7. .NET开源工作流RoadFlow-流程设计-保存与发布

    如果流程未设计完时可以先保存,以后再打开接着设计.点击工具栏上的保存按钮即可保存当前流程设计: 如果下次要接着设计,则可以打开该流程继续设计: 如果流程设计完成,可以点击安装按钮来发布流程,流程安装成 ...

  8. hdu 1575 Tr A

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1575 Tr A Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和), ...

  9. Erlang generic standard behaviours -- gen

    在分析 gen_server (或者是gen_fsm )之前,首先应该弄明白,gen 这个module . -module(gen). -compile({inline,[get_node/1]}). ...

  10. ASP.NET MVC掉过的坑_MVC初识及MVC应用程序结构

    APS.Net MVC 浅谈[转] 来自MSDN 点击访问 MVC 理论结构 模型-视图-控制器 (MVC) 体系结构模式将应用程序分成三个主要组件:模型.视图和控制器. ASP.NET MVC 框架 ...