转自:http://blog.csdn.net/zhuzhihai1988/article/details/7701998
第一种:
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;
这种设置出来的外观不好控制
第二种:
UIButton*rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,30,30)];
[rightButtonsetImage:[UIImageimageNamed:@"search.png"]forState:UIControlStateNormal];
[rightButtonaddTarget:selfaction:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]initWithCustomView:rightButton];
[rightButton release];
self.navigationItem.rightBarButtonItem= rightItem;
[rightItem release];
这种图片将填满button,大小可控
第三种:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];
如何让navigationItem.rightBarButtonItem隐藏消失?
self.navigationItem.rightBarButtonItem=nil;
即可实现