代码控制左右item:
UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom]; btnCancel.frame=CGRectMake(0, 0, 60, 30); btnCancel.titleLabel.font=[UIFont systemFontOfSize:13]; btnCancel.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; [btnCancel setTitle:@"返回" forState:UIControlStateNormal]; [btnCancel addTarget:self action:@selector(buttonCancel:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:btnCancel]; UIButton *btnCommit = [UIButton buttonWithType:UIButtonTypeCustom]; btnCommit.frame=CGRectMake(0, 0, 60, 30); btnCommit.titleLabel.font=[UIFont systemFontOfSize:13]; btnCommit.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight; [btnCommit setTitle:@"提交" forState:UIControlStateNormal]; [btnCancel addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:btnCommit];
其他的方法:
//第一种:这种设置出来的外观不好控制
UIImage *searchimage=[UIImage imageNamed:@"search.png"];
UIBarButtonItem *barbtn=[[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)];
barbtn.image=searchimage;
self.navigationItem.rightBarButtonItem=barbtn;
//第二种:这种图片将填满button,大小可控
UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
[rightButton setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];
[rightButton addTarget:self action:@selector(backto)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem= rightItem;
//第三种:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];
如何让navigationItem.rightBarButtonItem隐藏消失?
self.navigationItem.rightBarButtonItem=nil;即可实现
原文:http://www.cnblogs.com/mapanguan/p/5458865.html