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

原文链接:http://www.jianshu.com/p/5c74dfc94deb


UI界面的完美是一个好的产品的前提条件,最近开发在设置导航上左侧右侧的barbuttonItem 的时候由于设计想要距离左右有10的边距但是在更改customview的button的frame始终不起作用后来看下了关navigationItem的东西,从5.0以后系统多了两个属性,leftBarButtonItems和rightBarButtonItems两个数组类型的属性,也就是可以添加多个导航按钮,注意下api中提到的两点重要的:

  • leftBarButtonItems are placed in the navigation bar left to right with the first
    item in the list at the left outside edge and left aligned.
  • rightBarButtonItems are placed right to left 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];**









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值