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