【问题标题】:How to display image in toolbar using Objective-C如何使用 Objective-C 在工具栏中显示图像
【发布时间】:2015-04-12 20:14:44
【问题描述】:

我有一个带有以下代码的工具栏;我想添加一个显示“工具”的图像,称为“toolsIcon.png”。

下面是我的代码:

    //BottomBar 

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44,    self.view.frame.size.width, 44);
    [self.view addSubview:toolbar];
    [toolbar release];

    //TOOLBAR ITEMS
    //button 1
    UIBarButtonItem *tools = [[UIBarButtonItem alloc]   initWithTitle:@"Tools" style:UIBarButtonItemStyleBordered target:self    action:@selector(createToolView)];
    [tools setTag:0];



    //add the buttons to the toolbar
    NSArray *buttons = [NSArray arrayWithObjects: tools,nil];
    [toolbar setItems: buttons animated:YES];
    //memory management for buttons - don't waste!
    [tools release];

【问题讨论】:

  • 看起来你没有使用 ARC,你确实应该使用。还是您只是在某个地方找到了代码?
  • 停止使用它不再需要的版本试试这个。 stackoverflow.com/questions/9926978/…回复结果。
  • 添加图片的代码在哪里?
  • 这就是我的要求

标签: ios objective-c uitoolbar theos


【解决方案1】:

您可以使用图像创建 UIBarButtonItem 并添加它

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage: yourImage style: UIBarButtonItemStyleBordered target: nil action: nil];

【讨论】:

    【解决方案2】:

    由于您在谈论工具栏,我假设您在谈论底部栏,您有两个选择:

    1. 将 UIBarButtonItem 与您的图像一起用作纹理并移除用户交互。

    2. 为您的 png 创建一个包含 ImageView 的 UIView,并将其指定为工具栏的子级。

    如果我以后不再需要该图像作为按钮,我更喜欢第二种方法。

    您需要的代码非常标准...

    Objective-C

    UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 66, 33)];
    UIImageView *toolsImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Tools.png"]];
    
    toolsImage.frame = CGRectMake(0, 0, 66, 33);
    [customView addSubview: toolsImage];
    
    //And then you add it as a child of your toolbar...
    

    斯威夫特 3

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: 66, height: 33))
    let toolsImage =  UIImageView(image: UIImage(named: "Tools"))
    
    toolsImage.frame = CGRect(x: 0, y: 0, width: 66, height: 33)
    customView.addSubview(toolsImage)
    
    //And then you add it as a child of your toolbar...
    

    希望对您有所帮助! :)

    【讨论】:

      【解决方案3】:

      您可以使用图像初始化 UIBarButtonItem 实例并将其设置为 UIToolbar。在 UIViewController 的viewDidLoad 方法中插入以下代码。

      UIToolbar *toolbar = [[UIToolbar alloc] init];
      toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
      
      UIBarButtonItem *tool = [[UIBarButtonItem alloc]   initWithImage:[UIImage imageNamed:@"your_image"] style:UIBarButtonItemStylePlain target:self action:@selector(methodCalledOnClick)];
      [toolbar setItems: @[tool] animated:YES];
      [self.view addSubview:toolbar];
      

      【讨论】:

        【解决方案4】:

        最简单的方法是将 UIButton 放到 item 的 customView 中,如下所示:

        UIToolbar * toolbar = [UIToolbar new];
        toolbar.frame = CGRectMake(0, h-44, w, 44);
        [self.view addSubview:toolbar];
        
        UIButton * button = [UIButton new];
        button.frame = CGRectMake(0, 0, 44, 44);
        [button setImage:[[UIImage imageNamed:@"tools-128.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
        [button setTintColor:[UIColor redColor]];
        [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
        [button setImageEdgeInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
        
        //good
        UIBarButtonItem * toolItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        //bad
        UIBarButtonItem * itemB = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"tools-48.png"] style:UIBarButtonItemStylePlain target:self action:nil];
        [itemB setImageInsets:UIEdgeInsetsMake(8, 8, 8, 8)];
        
        //worse
        UIBarButtonItem * itemC = [UIBarButtonItem new];
        [itemC setBackgroundImage:[UIImage imageNamed:@"tools-48.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        [toolbar setItems:@[toolItem, itemB, itemC] animated:true];
        

        根据您想要实现的目标,滚动您自己的自定义工具栏可能是值得的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-21
          • 1970-01-01
          • 2022-01-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多