好像很多时候又回到了原点,刚开始接触iOS的时候,就接触到了backBarItem和leftBarItem。
*很多时候需要自己设点返回按钮的标题,系统给的返回键的标题分为两种情况。
前一个页面有标题的情况,系统返回键的标题默认为<标题;
前一个页面没有标题的情况,系统返回键默认为<Back;
*自己设定系统导航栏返回键标题有两个办法。
// 细节: 本界面上设置, 下个界面上显示
// 方式一
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
//方式二
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] init];
backButtonItem.title = @"返回";
self.navigationItem.backBarButtonItem = backButtonItem;
*导航栏的item全部由自己设定。比如:
UIButton *rightButton=[UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setFrame:CGRectMake(0, 0, 30, 32)];
[rightButton setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(onNaviBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBar = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
此时需要修改item与屏幕之间的边界:
- 考虑到CustomView一般为UIButton,如果该按钮上面是一个图片,仅为了显示效果,我们可以通过setImage: forState:方法设置button的背景图片(注意这里不是setBackgroundImage: forState:),然后通过setImageEdgeInsets:方法设置按钮图片和按钮各个边界的距离即可。按钮为文字的情况类似,对应的方法是setTitle: forState:和setTitleEdgeInsets:
UIButton *closeUpItem = [UIButton buttonWithType:UIButtonTypeCustom];
closeUpItem.frame = CGRectMake(0, 0, 44, 32);
[closeUpItem setTitleEdgeInsets:UIEdgeInsetsMake(0, -20, 0, 0)];
[closeUpItem setBackgroundColor:[UIColor redColor]];
[closeUpItem setTitle:@"关闭" forState:UIControlStateNormal];
[closeUpItem setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[closeUpItem addTarget:self action:@selector(closeUpClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *closeUpBarItem = [[UIBarButtonItem alloc] initWithCustomView:closeUpItem];
- 初始化一个用于控制间距的UIBarButtonItem实例negativeSpacer,并设置negativeSpacer的width属性的值,设为-5的时候,正好可以使按钮与屏幕边界值为0,以rightBarButtonItem情况为例
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
/**
* 设置frame只能控制按钮的大小
*/
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];
通过往中间插入spaceItem来控制间距。
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = -20;
UIButton *closeUpItem = [UIButton buttonWithType:UIButtonTypeCustom];
closeUpItem.frame = CGRectMake(0, 0, 44, 32);
[closeUpItem setTitle:@"关闭" forState:UIControlStateNormal];
[closeUpItem setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[closeUpItem addTarget:self action:@selector(closeUpClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *closeUpBarItem = [[UIBarButtonItem alloc] initWithCustomView:closeUpItem];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,closeUpBarItem,nil];
self.navigationController.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:space,closeUpBarItem, nil];
UIBarButtonItem *firstSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
firstSpace.width = -20;
UIButton *rightItem = [UIButton buttonWithType:UIButtonTypeCustom];
rightItem.frame = CGRectMake(0, 0, 44, 30);
[rightItem setTitle:@"前进" forState:UIControlStateNormal];
[rightItem setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[rightItem addTarget:self action:@selector(forwardClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightItem];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:firstSpace,rightBarItem,nil];
self.navigationController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:firstSpace,rightBarItem,nil];