1

我有一个应用程序,我想在它处于编辑模式时自定义创建文件夹按钮。

我使用以下代码来做到这一点:

UIBarButtonItem * addButton;
addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(createFolder)];
[addButton setTitleTextAttributes:dictionaryOfTitleTextAttributes forState:UIControlStateNormal];
[addButton setBackgroundImage:[[UIImage imageWithCGImage:[UIImage imageNamed:@"List/newfolder-normal.png"].CGImage scale:2 orientation:UIImageOrientationDown] resizableImageWithCapInsets:UIEdgeInsetsMake(4, 14, 4, 14)] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[addButton setBackgroundImage:[[UIImage imageWithCGImage:[UIImage imageNamed:@"List/newfolder-touched.png"].CGImage scale:2 orientation:UIImageOrientationDown] resizableImageWithCapInsets:UIEdgeInsetsMake(4,14,4,14)] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

但我无法删除加号创建新文件夹按钮,应用程序看起来像这样

在此处输入图像描述

如何删除加号?

4

1 回答 1

2

与您实现代码一样,您只需UIBarButtonItem使用initWithTitleBellow来定义您

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并设置CustomViewUIbarButtonItem执行此任务:-

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];
  }
}
于 2013-09-10T10:18:43.433 回答