与您实现代码一样,您只需UIBarButtonItem
使用initWithTitle
Bellow来定义您
addButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(createFolder:)];
示例代码:-
UIBarButtonItem * addButton;
addButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:@selector(createFolder:)];
//[addButton setTitleTextAttributes:di forState:UIControlStateNormal];
[addButton setBackgroundImage:[[UIImage imageWithCGImage:[UIImage imageNamed:@"redHeart.png"].CGImage scale:2 orientation:UIImageOrientationDown] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 14, 4, 14)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[addButton setBackgroundImage:[[UIImage imageWithCGImage:[UIImage imageNamed:@"blueheart.png"].CGImage scale:2 orientation:UIImageOrientationDown] resizableImageWithCapInsets:UIEdgeInsetsMake(4,14,4,14)] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
输出是
我建议您使用UIButton
并设置CustomView
为UIbarButtonItem
执行此任务:-
UIImage* yourImage = [UIImage imageNamed:@"newfolder-normal.png.png"];
CGRect frameimg = CGRectMake(0, 0, yourImage.size.width, yourImage.size.height);
someButton = [[UIButton alloc] initWithFrame:frameimg];
someButton.tag=1;
[someButton setBackgroundImage:yourImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(createFolder:)
forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];
addButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarbuttonITem=addButton;
[someButton release];
-(void)createFolder:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;
//If checked, uncheck and visa versa
[selectedButton setSelected:![selectedButton isSelected]];
if([selectedButton isSelected])
{
UIImage* yourImage = [UIImage imageNamed:@"List/newfolder-touched.png"];
[selectedButton setBackgroundImage:yourImage forState:UIControlStateNormal];
}
else
{
UIImage* yourImage = [UIImage imageNamed:@"List/newfolder-normal.png"];
[selectedButton setBackgroundImage:yourImage forState:UIControlStateNormal];
}
}