3

enter image description here

As shown above, I want to create an UIBarButtonItem with UIImageView and UILabel on a toolbar. I tried

UIButton *likecommButton = [UIButton buttonWithType:UIButtonTypeCustom];
    likecommButton.backgroundColor = [UIColor clearColor];
    [likecommButton addTarget:self action:@selector(likecommButtonClicked:) forControlEvents:UIControlEventTouchDown];


    UIImageView *likeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"like.png"]];
    likeImageView.frame = CGRectMake(0.0, 0.0, LikeCommentImageEdge, LikeCommentImageEdge);
    likeImageView.backgroundColor = [UIColor clearColor];
    [likecommButton addSubview:likeImageView];
    [likeImageView release];

    CGSize numberSize = [@"99" sizeWithFont:[UIFont fontWithName:@"Verdana-Bold" size:12] 
                                     constrainedToSize:CGSizeMake(20.0, 20.0)  
                                         lineBreakMode:UILineBreakModeTailTruncation];

    _likeNumberLabel = [[UILabel alloc] initWithFrame: CGRectMake(likeImageView.frame.size.width, 0.0, numberSize.width, numberSize.width)];
    _likeNumberLabel.backgroundColor = [UIColor clearColor];
    _likeNumberLabel.textColor = [UIColor whiteColor];
    _likeNumberLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:12];
    _likeNumberLabel.textAlignment = UITextAlignmentRight;
    _likeNumberLabel.lineBreakMode = UILineBreakModeClip;
    [likecommButton addSubview:_likeNumberLabel];

    UIImageView *commentImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comment.png"]];
    commentImageView.frame = CGRectMake(_likeNumberLabel.frame.origin.x + _likeNumberLabel.frame.size.width, 0.0, LikeCommentImageEdge, LikeCommentImageEdge);
    commentImageView.backgroundColor = [UIColor clearColor];
    [likecommButton addSubview:commentImageView];
    [commentImageView release];

    _commentNumberLabel = [[UILabel alloc] initWithFrame: CGRectMake(toolBarButtonWidth - numberSize.width, 0.0, numberSize.width, numberSize.width)];
    _commentNumberLabel.backgroundColor = [UIColor clearColor];
    _commentNumberLabel.textColor = [UIColor whiteColor];
    _commentNumberLabel.font = [UIFont fontWithName:@"Verdana-Bold" size:12];
    _commentNumberLabel.textAlignment = UITextAlignmentRight;
    _commentNumberLabel.lineBreakMode = UILineBreakModeClip;
    [likecommButton addSubview:_commentNumberLabel];

    likecommButton.frame = CGRectMake(0.0, 0.0, toolBarButtonWidth, numberSize.height);

    _likeCommCountButton = [[UIBarButtonItem alloc] initWithCustomView:likecommButton];
    _likeCommCountButton.width = toolBarButtonWidth;
    _likeCommCountButton.enabled = NO;

but can only got this

enter image description here

How to create UIBarButtonItem like the first image shows, include UIImageView and UILabel also have UIBarButtonItemStyleBordered style?

thanks


Update 20120506

This is a follow @R.A 's idea -- small toolbar

enter image description here

the problem of this I know how to set frame to toolbar but can't get the other two UIBarButtonItems "Like" and "Comment" height. then I can't set the small toolbar's height.

Then I tried to add all controllers, two imageview and two labels, into an UISegmentControl

enter image description here

As you see, the third uibarbuttonitem (the UISegmentControl one) is not like the other two. I set

segmentControl.tintColor = [UIColor clearColor]; 
    segmentControl.backgroundColor = [UIColor clearColor]; 

Not work.

I prefer @R.A 's one, but need a way to get the other two UIBarButtonItem's height to set for the third small toolbar's height.


Update 20120509

It took me too much time on it, I have to move on, I am now using UISegmentControl solution (which style is little different). I won't close this question. I will keep on study on it in the future. Many thanks for friends' help below, especially @R.A @vishiphone you guys gave me different thinking. Hope we can still stick together to dig out more question. I will continue this topic later. Hope you guys are still here! Thank you very much!

Jason Zhao
  • 1,278
  • 4
  • 19
  • 36
  • Actually when u add that two barbuttonItem to that toolbar itself, you have to set the frame for both like and comment button..And then add it to toolbar..After that, you have to add that total toolbar to a subview of a barButtonItem and then add that barButtonItem in your navigation bar.. – Dinesh Raja May 07 '12 at 04:49
  • I think You should remove the tint color from your navigation bar and transparency property too.. – Dinesh Raja May 08 '12 at 09:21

5 Answers5

2

Try these ones as normal/highlight images for UIButton - as a customView for UIBarButtonItem

(with stretchable width)

  1. BarButtonPressed;
  2. BarButtonPressed@2x
  3. BarButtonNormal
  4. BarButtonNormal@2x

BarButtonPressed BarButtonPressed@2x BarButtonNormal BarButtonNormal@2x

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
1

I got exactly, What they are doing.

In that image, they have created a UIToolBar with a small size and added three UIBarButtonItem's in that toolbar with 2 images and 1 label name respectively.

After that, adding that UIToolBar as a subView (or) customView of BarButtonItem with Bordered style.

But the trick is while adding those three buttons in that UIToolbar they are not having any border style for that Barbuttons(UIBarButtonStyleNone -> Like this But Not exactly).

So only it looks like it is having 2 images and a one label. Already i have did something like this. But not this much tricky..

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • Sorry @R.A I was on vacation. You said "After that, adding that UIToolBar as a subView (or) customView of BarButtonItem with Bordered style". Now the thing is how could you add a customView to UIBarButtonItem with bordered sytle? – Jason Zhao May 03 '12 at 03:45
  • not with border style..Add that toolbar as custom view or subview to that UIBarButtonItem. After or before adding that, set as like this yourBarButton.style = UIBarButtonItemStyleBordered; – Dinesh Raja May 03 '12 at 05:06
  • All right, I'll try later or tomorrow, will update you, thanks – Jason Zhao May 03 '12 at 05:12
  • I tried your way, but I'm blocking on how to set the small toolbar's frame, actual is how to set the toolbar's height. As the image I shown above, I need the small toolbar's height like the other two "like" and "comment" UIBarButtonItems – Jason Zhao May 05 '12 at 15:12
  • You can change the frame as you wanted.Just refer this link to change your toolbar frame http://stackoverflow.com/questions/2135407/is-there-a-way-to-change-the-height-of-a-uitoolbar – Dinesh Raja May 05 '12 at 18:55
  • I considered your way. and know what you mean, your way is good for add more items to UIToolBar or navigator. But not for this case. Cos the way you implemented still couldn't set border style. (I tried a lot). BtW, are you Indian? I just watched a movie "3 idiots". I really like this movie, SO GOOD! but the name :P , do the leading man and heroine famous in India? – Jason Zhao May 08 '12 at 06:24
  • @JasonZhao Oh that's nice.. Yep.. One of the famous.. I think this is not the right place to speak that.. You can contact my mail. It's in my profile. If you could send your code that your tried and i think i can help you too.. – Dinesh Raja May 08 '12 at 09:18
  • You are still here, I can't find a reason give up. Never give up! :D – Jason Zhao May 09 '12 at 06:24
1

enter image description hereTry this lines of code I think this will be helpful for you.

UIView *BtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0,70,35)];
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:@"firstImage.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(compete) forControlEvents:UIControlEventTouchUpInside];



UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(52, -4, 21, 21)];
[imageView setImage:[UIImage imageNamed:@"Secongimage.png"]];
UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)];
badge_Label.backgroundColor=[UIColor clearColor];
badge_Label.font=[UIFont systemFontOfSize:12];
[badge_Label setText:@"20"];
[imageView addSubview:badge_Label];


[BtnView addSubview:myButton];
[BtnView addSubview:imageView];
[myButton release];

UIBarButtonItem *CompeteButton = [[UIBarButtonItem alloc]initWithCustomView:BtnView]; 

self.navigationItem.rightBarButtonItem = CompeteButton;


UIView *leftBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
UIButton *menu_Button = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[menu_Button setFrame:CGRectMake(0,3,37,37)];
[menu_Button setImage:[UIImage imageNamed:@"menubut.png"] forState:UIControlStateNormal];
[menu_Button addTarget:self action:@selector(list) forControlEvents:UIControlEventTouchUpInside];

[leftBtnView addSubview:menu_Button];

UIBarButtonItem *menuButton = [[UIBarButtonItem alloc]initWithCustomView:leftBtnView]; 
self.navigationItem.leftBarButtonItem = menuButton;
vishiphone
  • 750
  • 7
  • 14
  • I tried this before, the problem is set UIButton type UIButtonTypeCustom, the UIButton has no border style. Then add this to UIBarButtonItem with initWithCustomView, the initWithCustomView has no border style, although we can set menuButton.style = UIBarButtonItemStyleBordered, but seems not work, the menuButton still no border style. And the last thing is, I still donno how to get menu_Button's height as the other two UIBarButtonItems, is there a way to get UIBarButtonItem's height? I got some, but not work for this case – Jason Zhao May 07 '12 at 05:18
  • First thing If you add your button on left bar button or right bar button item I am sure that will be round rect only.try this code exactly and tem tell me what are u getting?and if you not getting then use round rect image only if you want image I will send you.and your next question ans you can change your bar button height.just add view and image which image height you want just tell me i send you code for that. – vishiphone May 07 '12 at 05:27
  • Thanks, I tried just now, got the exact same result as my original question first image attach. And if you check my original question, you can see I was tried your way. BTW, I am trying to add UIBarButtonItem to a UIToolBar not navigation controller. P.S. it is great that you can send me your sample project code, then I can try on local. – Jason Zhao May 07 '12 at 07:41
  • If this is helpful for you then please mark it correct and up my answer arrow so it will be helpful for other also and it will increase your expect rate also thanks. – vishiphone May 07 '12 at 07:44
  • Sorry @vishiphone, I didn't describe it clearly. I mean "exact same result" as I pasted in my original question first attach image shown. No border style. Means your if you read my original question, you could see, I was trying your way and got the result like first attach image shown. And just now, I tried again, with your code. got the same result. See? – Jason Zhao May 07 '12 at 08:02
  • I send you image for button add that also. – vishiphone May 07 '12 at 08:24
  • Use above image you got the output in round rect only. – vishiphone May 07 '12 at 08:26
  • Woow, I see, but I don't want to use image. You can see my update in my original question, I've already got one (UISegmentControl) which is very close to expecting. Now the worst, I can set three UISegmentControl to three UIBarButtonItem to let their style look same. But don't want to use image, thanks anyway @vishiphone – Jason Zhao May 07 '12 at 08:37
  • If my information is helpful for you just up the arrow for me and from next time in here if any one help you then you mark the arrow up so from next time that will be good for you. – vishiphone May 07 '12 at 08:45
  • Thanks jason and tell me youe next problem if you dont want image then. – vishiphone May 07 '12 at 08:45
  • and for your output you do the wrong thing if you want then i can show you exact that output using image only because if you add segment control that will be problem in next view controller and i dint know that will be acceptable or not. – vishiphone May 07 '12 at 08:48
  • ok, suppose I am gonna use image, I need to set the image container (maybe a UIButton a UIImageView or UIView) height. And the height I think better to get from the other two UIBarButtonItems, not a hard code height. So we back to the problem: how to get the other two UIBarButtonItems' height? – Jason Zhao May 07 '12 at 09:38
  • just increase the view height and your bar button height buuump.. :) if you I want i can send you whole code try once.you just want three image. – vishiphone May 07 '12 at 09:42
  • ok I will update my code just try this code.and inform mr its good for you or not but i cant send you image here so once you use any image and set the frame according your requirement. – vishiphone May 07 '12 at 10:03
  • Try above code for your left and right button and you set your button height what you want. – vishiphone May 07 '12 at 10:07
  • you see in my code my button on view so you can adjust your view height button height will be change. – vishiphone May 07 '12 at 10:09
  • I'm doing another thing, later of today or tomorrow will update you for this thread. – Jason Zhao May 08 '12 at 06:16
0

I think you missed setting frame for each subview.Try to add with Frames then you will get as you want.

Manikandan
  • 107
  • 2
  • 12
  • Yes , i have seen Create a View in that add Like Image Button, Count Label and Comment Image View and create a BarButtonItem using that View. – Manikandan Apr 26 '12 at 14:02
0

You should create a UIView which will be the wrapper for the UILabel and the UiImageView, add them as subviews of this UiView, then use like above :

_likeCommCountButton = [[UIBarButtonItem alloc] initWithCustomView:_wrapper_view_];

And don't forget to correctly set the subviews' frames.

Templar
  • 1,694
  • 1
  • 14
  • 32
  • I tried, just same as you said, and got the same, I want that button with UIBarButtonItemStyleBordered style, same style of the other two UIBarButtonItem – Jason Zhao Apr 26 '12 at 13:49
  • then add `likeBtn.style = UIBarButtonItemStyleBordered;` after the init line. – Templar Apr 26 '12 at 13:54
  • Btw, is `toolBarButtonWidth` positive and not 0 ? Try with some constant frames for all elements to see where you made the mistake , like `CGRectMake(0,0, 20,20);` – Templar Apr 26 '12 at 14:20