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

  • 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];**

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 文 |小迷图 | Louie Beans 炒股的乐趣是,是不是会遇到“还有这种操作”的奇观。 昨晚,在美上市的中国...
    东三环的财小迷阅读 299评论 0 0
  • 2016冬,迎来了我青春里的第一场雪,它来得美丽、意外,以及整个城市都措手不及。不过欣赏雪景也是要付出代价的,今年...
    分外瑶娆阅读 297评论 0 0
  • 每一次提笔都不知从何下手,脑袋一片空白,就像在这个尴尬的年龄一样,一无所有,一事无成,一片迷茫…… 没想过是这个结...
    触及不可阅读 172评论 0 0

友情链接更多精彩内容