2012-04-26 2 views
2

enter image description hereどのようにUIImageViewでUIBarButtonItemを作成し、UILabel

上記のように、私は、ツールバー上の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; 

を試みたが、最初の画像が示すようにUIBarButtonItemを作成するために、どのようにこの

enter image description here

を得たことができ、UIImageViewとUILabelもスタイルをUIBarButtonItemStyleBorderedている含まれ?

おかげ


アップデート20120506

これは従って@RAのアイデア - 小さなツールバー

enter image description here

私はフレームを設定する方法を知っている、この問題他の2つのUIBarButtonItemsを「好き」と「コメント」の高さにすることはできません。私は小さなツールバーの高さを設定することはできません。

は、その後、私はUISegmentControl

enter image description here

ご覧のように、第3 uibarbuttonitem(UISegmentControl 1)は他の二つのようではないにすべてのコントローラ、2つのImageViewのと2つのラベルを、追加しようとしました。私は

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

は動作しません。

@ R.Aのほうが好きですが、他の2つのUIBarButtonItemの高さを3番目の小さなツールバーの高さに設定する方法が必要です。


アップデート20120509

それはそれで私にあまりにも多くの時間を要し、私が上に移動する必要があり、私は今、(少し異なっているスタイル)UISegmentControlソリューションを使用しています。私はこの質問を終了しません。私は将来それについて勉強し続けます。下の友人の助け、特に@ R.A @vishiphoneに多くのおかげです、皆さんは私に別の考えを与えました。私たちはまだまだ一緒にこだわり、より多くの質問を掘り起こすことができます後でこのトピックを続けます。あなたたちはまだここにいることを願っています!どうもありがとうございました!

+0

2倍likeボタンとコメントボタンの両方のために。そして、それをツールバーに追加します。その後、その合計ツールバーをサブビューに追加する必要がありますあなたのナビゲーションバーにbarButtonItemを追加してください。 –

+0

私は、あなたのナビゲーションバーと透明度のプロパティから色合いの色を削除する必要があると思います。 –

答えて

0

私は、あなたが望むように得るフレームを追加するために各サブビューのためにフレームを設定することを忘れたと思う。

+0

私のコードを見ることができます。私はすべてのコントローラのためにフレームを設定しました –

+0

はい、私は、画像のようなボタンを追加し、ラベルを数え、画像を表示し、そのビューを使ってBarButtonItemを作成するビューを作成するのを見ました。 – Manikandan

0

あなたはその後、上記のように使用し、このUiViewのサブビューとして追加、UILabelUiImageViewのラッパーになりますUIViewを作成する必要があります。

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

、正しくすることを忘れないでくださいサブビューのフレームを設定します。

+0

私は同じことをやりましたが、同じことをやってみました。UIBarButtonItemStyleBorderedスタイルのボタン、他の2つのUIBarButtonItemの同じスタイル –

+0

を追加し、init行の後に 'likeBtn.style = UIBarButtonItemStyleBordered; 'を追加します。 – Templar

+0

Btwは、 'toolBarButtonWidth'が正であり、0ではありませんか? 'CGRectMake(0,0,20,20);'のような間違った箇所を見るために、すべての要素に対していくつかの定数フレームを試してみてください。 – Templar

1

私は彼らがやっていることを正確に得ました。その画像において

、それらは小さなサイズでUIToolBarを作成し、2枚の画像と1ラベル名respectivelyとそのtoolbarUIBarButtonItem'sを追加しました。

その後、UIToolBarをサブビュー(または)のカスタムビューBarButtonItem with Bordered styleとして追加します。

しかしtrick、彼らはそのBarbuttonsためnot having any border styleであることUIToolbarにこれら3つのボタンを追加するときである(UIBarButtonStyleNone - >このようにバット・ノット正確に)。

2つの画像と1つのラベルがあるように見えます。すでに私はこのようなことをしています。しかしこれほどトリッキーではありません。

+0

あなたは私の答えをチェックアウトしましたか?正しいyarです –

+0

申し訳ありません@RA私は休暇中でした。あなたは "その後、そのUIToolBarをBordereredスタイルのBarButtonItemのsubView(または)customView"として追加します。これで、どうやってUBarButtonItemにボーダリングされたsytleでcustomViewを追加することができましたか? –

+0

borderスタイルではありません。そのツールバーをカスタムビューまたはそのUIBarButtonItemへのサブビューとして追加してください。それを追加する前または追加する前に、このように設定しますyourBarButton.style = UIBarButtonItemStyleBordered; –

1

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

+0

私はこの問題をUIButton型UIButtonTypeCustomに設定していますが、UIButtonには境界線スタイルがありません。これをinitWithCustomViewでUIBarButtonItemに追加すると、initWithCustomViewにはボーダースタイルはありませんが、menuButton.style = UIBarButtonItemStyleBorderedを設定できますが、動作しないようですが、menuButtonにはまだ枠線スタイルはありません。そして最後のことは、私はまだ他の2つのUIBarButtonItemsとしてmenu_Buttonの高さを取得する方法をdonno、UIBarButtonItemの高さを取得する方法はありますか?私はいくつかを持っていますが、この場合のために働いていません –

+0

最初のものあなたが左のバーボタンまたは右のバーボタンの項目にあなたのボタンを追加する場合、私は丸いrect.tryこのコードを正確にすると確信していますあなたが画像を必要とする場合は丸い矩形の画像を使用していない場合は、私はyou.andあなたの次の質問ansをあなたはバーの高さを変更することができますansを送信します。それのために。 – vishiphone

2

UIButtonの正常/ハイライト画像としてこれらのものを試してください - (伸縮幅)UIBarButtonItem

ためのCustomView

  1. BarButtonPressedとして;
  2. BarButtonPressed @ 2xの
  3. BarButtonNormal
  4. BarButtonNormal @ uはそのツールバー自体に2 barbuttonItemは、あなたがフレームを設定する必要が追加したときに実際に

BarButtonPressed BarButtonPressed@2x BarButtonNormal BarButtonNormal@2x

関連する問題