如何使用的UIImageView和的UILabel

问题描述:

enter image description here如何使用的UIImageView和的UILabel

如上图所示创建的UIBarButtonItem,我想在工具栏上创建的UIImageView和一个的UILabel的UIBarButtonItem。我试图

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; 

却只能得到这个

enter image description here

如何创建的UIBarButtonItem像第一张图片显示,包括的UIImageView和的UILabel也UIBarButtonItemStyleBordered风格?

感谢


更新20120506

这是一个后续@RA的想法 - 小工具栏

enter image description here

这个我知道如何设置框架问题到工具栏,但无法获得其他两个UIBarButtonItems“喜欢”和“评论”的高度。那么我无法设置小工具栏的高度。

然后我试图给所有控制器,2个imageview的和两个标签,添加到UISegmentControl

enter image description here

正如你看到的,第三的UIBarButtonItem(在UISegmentControl之一)并不像其他两个。 I set

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

不工作。

我更喜欢@ R.A的一个,但需要一种方法来获得另外两个UIBarButtonItem的高度,以设置第三个小工具栏的高度。


更新20120509

我花了太多时间在上面,我必须继续前进,我现在用的UISegmentControl溶液(哪种风格略有不同)。我不会关闭这个问题。我将来会继续研究它。非常感谢以下朋友的帮助,特别是@ R.A @vishiphone你们给我不同的想法。希望我们仍然能够一起挖掘更多的问题。稍后我会继续讨论这个话题。希望你们还在这里!非常感谢你!

+0

其实当u添加两个barbuttonItem到工具栏本身,你必须设置框架为喜欢和评论按钮..然后将其添加到工具栏..之后,你必须将总工具栏添加到子视图的barButtonItem,然后在你的导航栏中添加该barButtonItem .. – 2012-05-07 04:49:36

+0

我认为你应该从你的导航栏和透明属性中删除色调颜色.. – 2012-05-08 09:21:05

我想你错过了每个子视图的设置框架。尝试添加框架,然后你会得到你想要的。

+0

我做过了,你可以看到我的代码。我为每个控制器设置框架 – 2012-04-26 13:47:19

+0

是的,我已经看到创建一个视图,添加像图像按钮,计数标签和注释图像视图,并使用该视图创建一个BarButtonItem。 – Manikandan 2012-04-26 14:02:58

您应该创建一个UIView这将是UILabelUiImageView包装,将其添加为这个UiView的子视图,然后用像上面:

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

而且不要忘了正确设置子视图的框架。

+0

我试过了,就像你说的一样,并得到了相同的,我想UIBarButtonItemStyleBordered风格,另外两个的UIBarButtonItem – 2012-04-26 13:49:28

+0

的相同的风格,按钮,然后加上'likeBtn.style = UIBarButtonItemStyleBordered;'初始化行之后。 – Templar 2012-04-26 13:54:35

+0

顺便说一句,是'toolBarButtonWidth'正数而不是0?尝试用所有元素的一些常量帧来查看你犯的错误,比如'CGRectMake(0,0,20,20);' – Templar 2012-04-26 14:20:03

我得到了,他们在做什么。

在该图像,他们已经创建了一个UIToolBar具有小的尺寸和在该toolbar与2倍的图像和1个标签名称respectively增加了三个UIBarButtonItem's

之后,将UIToolBar添加为子视图(或)customView的BarButtonItem with Bordered style

trick是同时在UIToolbar添加这些三个按钮,它们是not having any border style为Barbuttons(UIBarButtonStyleNone - >喜欢,但是不完全)。

所以只有它看起来像是有2个图像和一个标签。我已经做了这样的事情。但没有这么多棘手..

+0

你检查了我的答案吗?是正确的yar – 2012-04-28 10:42:06

+0

对不起@RA我在度假。你说:“之后,将UIToolBar添加为具有边框样式的子视图(或)BarButtonItem的customView”。现在的事情是,你怎么可以添加customView到UIBarButtonItem与边界的情况? – 2012-05-03 03:45:35

+0

不带边框样式。将该工具栏添加为该UIBarButtonItem的自定义视图或子视图。添加之后或之前,设置为像这样yourBarButton.style = UIBarButtonItemStyleBordered; – 2012-05-03 05:06:32

enter image description here试试这个代码行,我认为这将是对你有帮助。

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; 
+0

@jason如果它以我们的形式为你工作。 – vishiphone 2012-05-07 04:20:36

+0

我以前试过这个,问题是设置UIButton类型UIButtonTypeCustom,UIButton没有边框样式。然后使用initWithCustomView将此添加到UIBarButtonItem,initWithCustomView没有边框样式,虽然我们可以设置menuButton.style = UIBarButtonItemStyleBordered,但似乎不起作用,menuButton仍然没有边框样式。最后一件事是,我仍然不知道如何获得menu_Button的高度作为其他两个UIBarButtonItems,有没有办法获得UIBarButtonItem的高度?我得到了一些,但不适用于这种情况 – 2012-05-07 05:18:29

+0

第一件事如果你在左边的酒吧按钮或右边的酒吧按钮项目上添加你的按钮,我相信这将是圆形矩形only.try此代码完全和tem告诉我你在做什么?如果你没有得到然后使用圆形矩形图像只有当你想要的图像我会发送给你。你的下一个问题,你可以改变你的酒吧按钮height.just添加视图和图像你想要的图像高度只是告诉我我给你的代码为了那个原因。 – vishiphone 2012-05-07 05:27:31

尝试这些的正常/高光图像进行的UIButton - 作为一个customView为UIBarButtonItem

(具有可拉伸宽度)

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

BarButtonPressedBarButtonPressed@2xBarButtonNormalBarButtonNormal@2x