9

I have a toolbar with the code below; I would like to add an image that displays with "Tools" Called "toolsIcon.png".

Below is my code:

    //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];
Unheilig
  • 16,196
  • 193
  • 68
  • 98

4 Answers4

7

You can create UIBarButtonItem with an image and add it

UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithImage: yourImage style: UIBarButtonItemStyleBordered target: nil action: nil];
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
3

Since you are talking of a toolbar I assume that you are talking about the bottom bar you have two choices:

  1. Use a UIBarButtonItem with your image as texture and remove the user interaction for it.

  2. Create a UIView which contains the ImageView for your png and assign it as a child of the toolbar.

I prefer the second approach if I'll never need that image as a button in the future.

The code you need is pretty standard...

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...

Swift 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...

I hope to have been helpful! :)

2

enter image description here

The easiest way is to drop a UIButton into the customView of the item, like so:

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];

It might be worthwhile rolling your own custom toolbar, depending on what you want to achieve.

Johnny Rockex
  • 4,136
  • 3
  • 35
  • 55
2

You can initialize UIBarButtonItem instance with an image and set it to UIToolbar. Insert the below code in viewDidLoad method of UIViewController.

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];
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22