添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

先谢谢标哥啦 http://blog.csdn.net/woaifen3344/article/details/24793087

在设置navigationItem的leftBarButtonItem或rightBarButtonItem时,

用CustomView初始化UIBarButtonItem,不论怎么设置CustomView的frame,

添加到导航条上之后总是和屏幕边界有一定的间距(5pix),

如何自由调整这个间距呢?

下面介绍两种方法:

1、不用直接设置rightBartButtonItem而是设置rightBartButtonItems,并且第一个item设置为一个占位。

  1. - ( UIButton *)addRightItemWithTitle:( NSString *)title action :( SEL )action {
  2. UIButton *button = [UIButton buttonWithType :UIButtonTypeCustom];
  3. CGSize size = [title sizeWithFont :[UIFont systemFontOfSize : 1 6 ]];
  4. //! 这里需要根据内容大小来调整宽度
  5. button .frame = CGRectMake( 0 , 0 , size .width <= 1 0 ? 7 0 : size .width + 1 0 , 4 4 );
  6. button .titleLabel .textColor = [UIColor whiteColor ];
  7. button .titleLabel .font = [UIFont systemFontOfSize : 1 6 ];
  8. button .titleLabel .textAlignment = NSTextAlignmentRight;
  9. [button setTitle :title forState :UIControlStateNormal];
  10. /**
  11. *  width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-15时,间距正好调整
  12. *  为10;width为正数时,正好相反,相当于往左移动width数值个像素
  13. */
  14. UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc ]
  15. initWithBarButtonSystemItem :UIBarButtonSystemItemFixedSpace
  16. target :nil action :nil ];
  17. negativeSpacer .width = - 1 5 ;
  18. [button addTarget : self action :action forControlEvents :UIControlEventTouchUpInside];
  19. UIBarButtonItem *backItem = [[UIBarButtonItem alloc ] initWithCustomView :button];
  20. self .navigationItem .rightBarButtonItems = @[negativeSpacer, backItem ];
  21. return button;
  22. }
2、如果是只有图片,那么通过设置

[button setImageEdgeInsets : UIEdgeInsetsMake ( 0 , - 15 , 0 , -15 )];这样也可以调整

  1. - ( UIButton *)addRightItemWithImage:( NSString *)imageName action :( SEL )action {
  2. UIButton *button = [UIButton buttonWithType :UIButtonTypeCustom];
  3. UIImage *image = [UIImage imageNamed :imageName];
  4. button .frame = CGRectMake( 0 , 0 , image .size .width , image .size .height );
  5. // 这里需要注意:由于是想让图片右移,所以left需要设置为正,right需要设置为负。正在是相反的。
  6. // 让按钮图片右移15
  7. [button setImageEdgeInsets :UIEdgeInsetsMake( 0 , 1 5 , 0 , - 1 5 )];
  8. [button setImage :image forState :UIControlStateNormal];
  9. [button addTarget : self action :action forControlEvents :UIControlEventTouchUpInside];
  10. button .titleLabel .font = [UIFont systemFontOfSize : 1 6 ];
  11. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc ] initWithCustomView :button];
  12. self .navigationItem .rightBarButtonItem = rightItem;
  13. return button;
先谢谢标哥啦 http://blog.csdn.net/woaifen3344/article/details/24793087在设置navigationItem的leftBarButtonItem或rightBarButtonItem时,用CustomView初始化UIBarButtonItem,不论怎么设置CustomView的frame,添加到导航条上之后总是和屏
我们发现,在设置navigation Item 的leftBar Button Item 或 right Bar Button Item 时,用CustomView初始化 UI Bar Button Item ,不论怎么设置CustomView的frame,添加到 导航 条上之后总是和 屏幕 边界 有一定的 间距 (5pix),如何自由 调整 这个 间距 呢? 初始化一个用于控制 间距 的 UI Bar Button Item 实例negativeSpacer,并设置negativeSpacer的width属性的值,设为-5的时候,正好可以使按钮与 屏幕 边界 值为0,以rig
- (void)addNavigation Item { UIButton *btnOne = [[ UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; btnOne.backgroundColor = [ UI Color redColor]; UI Bar Button Item *leftOne = [[ UI BarButt
UI Navigation Item - iOS 7Spacing 用于修复 iOS 7 UI Navigation Item 间距 的类别。 iOS 5/6 上的 leftBar Button Item 位置 iOS 7 上的 leftBar Button Item 位置 修复了 iOS 7 上的 leftBar Button Item 位置 除了在 *-Prefix.pch 文件中包含此类别外,您无需执行任何其他操作: #ifdef __OBJC__ #import < UI Kit> #import <Foundation> #import " UI Navigation Item + iOS 7Spacing.h" #endif 在 iOS 6/7 上经过全面测试。
iOS 11版本由于对于Nav层级结构的改变,导致以前的 方法 无法达到理想的移动效果,使顶部的按钮完全靠左,或者是靠右. 在 iOS 11之前保持原有方式进行设置, iOS 11之后进行额外的边距约束修改达到移动效果. 从viewDebug的界面上观察可以看到需要将 UIButton BarStackView 距离 左边和 右边 的16的约束改为0即可. 配置 导航 器view代码 //0:leftBar Button Item s,1: right Bar Button Item s - (void)initBar Item :( UI View*)view withType:(int)type{
界面的 导航 栏 距离 边界 感觉有点圆,然后就找了一下怎么修改间隙 文字 到 边界 的 距离 ,果然找到了。 UIButton *btn = [ UIButton button WithType: UIButton TypeRoundedRect]; * 设置frame只能控制按钮的大小 btn.frame= CGRectMake(0, 0, 40, 44);
今天项目有个需求,要求右侧导行栏添加三个控钮,想到用 right Bar Button Item s包三个 right Bar Button Item , 结果控件之间的 间距 没法 调整 ,试过很多种 方法 都不行,结合网上一些思路及自己拓展,最终解决方案是在两 个控件中间加个占位的 Item .代码如下。
相信很多同学都知道在 iOS 7之后 调整 导航 栏两侧按钮 距离 左右 间距 ,其实就是在左右bar Button Item 的数组中添加一个宽度为负的占位 item 。 - (void)addLeftBar Button Item :( UI Bar Button Item *)leftBar Button Item UI Bar Button Item *space = [[ UI Bar Button Item alloc] ...