0

Actually i want to place image and label on the toolbar as a UIBarButtonItem and provide a clickable effect to that Button.

So what i have done here is I have created one custom view and placed Image and Label on the same custom view and finally placed this custom view as UIBarButtonItem CustomView.

But when i set target and action for the same UIBarButtonItem, it is not calling the selector method.

The entire code is below.

Can anybody suggest me what's the mistake in my code ? and is there any other approach to achieve the same ????

Early suggestions would be much appreciated.

- (void)viewDidLoad

{

[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];


 UIView *customView = [[UIView alloc]
initWithFrame:CGRectMake(0,0,self.navigationController.toolbar.frame.size.width,self.navigationController.toolbar.frame.size.height)];

customView.backgroundColor = [UIColor colorWithRed:62.0/255.0 green:187.0/255.0 blue:150.0/255.0 alpha:1.0];

[self.navigationController.toolbar addSubview:customView];


UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(60,0,44,44)];
imgView.image = [UIImage imageNamed:@"documentImage.png"];
[customView addSubview:imgView];


   UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(104,0,145,44)];

   lbl.text = @"Scan Document";

   lbl.textAlignment = NSTextAlignmentLeft;

    [customView addSubview:lbl];

UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:customView];
bar.target = self;
bar.action = @selector(scanDocument);
self.toolbarItems = [NSArray arrayWithObjects:bar, nil];
 }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143

2 Answers2

0

If you want to create button right hand side here is the code. i have already implemented it and working fine.

  //Button Right side
    UIImage *imgFavRest = [UIImage imageNamed:@"documentImage.png"];
    UIButton *cart = [UIButton buttonWithType:UIButtonTypeCustom];
    [cart setImage:imgFavRest forState:UIControlStateNormal];
    cart.frame = CGRectMake(0, 0, imgFavRest.size.width, imgFavRest.size.height);
    [cart addTarget:self action:@selector(showCart) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:cart];
    self.navigationItem.rightBarButtonItem = rightBtn;

Then use method like this . that's all

-(void)cartItemCount{
// Method
}
Im Batman
  • 1,842
  • 1
  • 31
  • 48
  • Image should be displayed before the Label. So,it should be like Image first Label Next. I wanna apply target-action to both and provide the clickable effect before calling selector method. – Nagendra K Feb 26 '16 at 09:41
  • Thank you @Cade. It is working fine but i wanna always show this button at the bottom of viewcontroller always on top (means i have a scrollview on the viewcontroller i wanna always show this botton on top of scrollview). – Nagendra K Mar 01 '16 at 05:03
0
    float textwidth = 50; // according to your text
    UIImage *image = [UIImage imageNamed:@"logout"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.bounds = CGRectMake( 10, 0, image.size.width+textwidth, image.size.height );
    [btn addTarget:self action:@selector(scanDocument) forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:image forState:UIControlStateNormal];
    [btn setTitle:@"test" forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 5.0, 0.0, 0.0)];

    //Adjust the coordinates and size of your button as your requirement.This is a sample code to guide you.
    //PS:Take a UIButton in the nib file>Make it a custom button>Connect the IBOutlet to your custom button in the nib file.Thats it.

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightButton;

I see very complicated answers, all of them using code. However, if you are using Interface Builder, there is a very easy way to do this:

  • Select the button and set a title and an image. Note that if you set the background instead of the image then the image will be resized if it is smaller than the button.IB basic image and title
    enter image description here
  • Set the position of both items by changing the edge and insets. You could even control the alignment of both in the Control section. enter image description here
  • IB position set IB Control set enter image description here

You could even use the same approach by code, without creating UILabels and UIImages inside as other solutions proposed. Always Keep It Simple!

EDIT: Attached a small example having the 3 things set (title, image and background) with correct insets Button preview

Note:: UIBarButtonItem inherits from UIBarItem and NSObject so it doesn't know anything about touches. It would be nice if the docs mentioned that the action and target properties only apply if the custom view is a UIButton.

Mahesh
  • 956
  • 10
  • 17