美文网首页推送ios iOS
iOS 关于导航两侧按钮距离左右侧边距的修改

iOS 关于导航两侧按钮距离左右侧边距的修改

作者: Z_Lukas | 来源:发表于2015-12-18 16:55 被阅读8164次
  • UI界面的完美是一个好的产品的前提条件,最近开发在设置导航上左侧右侧的barbuttonItem 的时候由于设计想要距离左右有10的边距但是在更改customview的button的frame始终不起作用后来看下了关navigationItem的东西,从5.0以后系统多了两个属性,leftBarButtonItems和rightBarButtonItems两个数组类型的属性,也就是可以添加多个导航按钮,注意下api中提到的两点重要的:
    • leftBarButtonItems are placed in the navigation bar<b> left to right </b>with the first
      item in the list at the left outside edge and left aligned.
    • rightBarButtonItems are placed <b> right to left </b>with the first item in the list at
      the right outside edge and right aligned.

左侧的是从左到右边按照数组里的次序、右侧是从右向左 也就是说数组中的第一个放最右边,左侧的数组中的是第一个放最左边

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btn.frame= CGRectMake(0, 0, 40, 44);  
[btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];  
UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn];  
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];  

/**
width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和  边界间距为5pix,所以width设为-5时,间距正好调整为0;width为正数 时,正好相反,相当于往左移动width数值个像素
*/
negativeSpacer.width = -5;   
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];  

补充:导航控制器两侧的按钮颜色
self.navigationController.navigationBar.tintColor = [UIColorwhiteColor];**

相关文章

网友评论

  • 27639726f8b9:看了好几篇文章都是这样类似的方法,可是为什么我用了不管用,间隙依然保持原样。
  • 135d7f3c2206:只有这样,将2个按钮往右一起移动有效:UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width =-15;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,rightSubmitBarButon,rightRePaintBarButon, nil];
  • 135d7f3c2206:请问,为什么我用上面评论中的方法,没有缩小2个按钮之间的距离,代码如下 UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width =-15;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:rightSubmitBarButon,negativeSpacer,rightRePaintBarButon, nil];
    Z_Lukas:@小灰灰wf 你注意下顺序,左侧的按钮数字是依次从左开始添加顺序,右侧的按钮数组是依次从右边添加的顺序
  • 施主小欣:请问 如果我右边是两个自定义按钮 我需要改两个按钮之间的间距 这个需要怎么改?
    施主小欣:@litterbird 已解决谢谢~!
    e79e72cbb5a9:@施主小欣 你在两个按钮之间放一个 UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    你可以设置leftbarItem的 width属性用来控制两个按钮之间的间距;
    self.navigationItem.leftBarButtonItems =[NSArray arrayWithObjects:But1,leftbarBut,But2, nil];

本文标题:iOS 关于导航两侧按钮距离左右侧边距的修改

本文链接:https://www.haomeiwen.com/subject/dwmphttx.html