2013-10-31 93 views
5

我想添加一個customBadge作爲一個UIButton的子視圖 -添加子視圖的UIButton

這是我到目前爲止的代碼 -

//msg count initiaition 
//CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"2"]; 
CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"2" 
               withStringColor:[UIColor whiteColor] 
               withInsetColor:[UIColor redColor] 
               withBadgeFrame:YES 
              withBadgeFrameColor:[UIColor redColor] 
                withScale:2.0 
                withShining:YES]; 

    // Set Position of Badge 1 
[customBadge1 setFrame:CGRectMake(self.view.frame.size.width/2-customBadge1.frame.size.width/2+_MsgHeadBtn.frame.size.width/2, 110, customBadge1.frame.size.width, customBadge1.frame.size.height)]; 
//add badge to view 
[_MsgHeadBtn addSubview:customBadge1]; 

按鈕我試圖加子視圖爲_MsgHeadBtn,這是以下屏幕截圖頂部LH上的電子郵件圖標。 我試圖讓自定義徽章在電子郵件圖標的右側顯示輕微的位置 - 但最終會在截圖中顯示結果!

enter image description here

誰能提供任何意見,以我要去哪裏錯了!?

回答

5

問題在您的setFrame:方法中。您正在使用self.view.frame.size.width

檢查與此代碼:

[customBadge1 setCenter:CGPointMake(_MsgHeadBtn.frame.size.width, 0)]; 
[_MsgHeadBtn addSubview:customBadge1]; 

[customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width, 0, customBadge1.frame.size.width, customBadge1.frame.size.height)]; 
[_MsgHeadBtn addSubview:customBadge1]; 
+0

我會說:'[customBadge1 SETFRAME:CGRectMake(_MsgHeadBtn.frame .size.width,0,customBadge1.frame.size.width,customBadge1.frame.size.height)];' – sergio

+0

@sergio:感謝您的信息。我只是修改了我的答案:) –

+0

作品 - 不錯的一個! – Dancer

1

調整你的框架如下圖所示:

[customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width-customBadge1.frame.size.width,-customBadge1.frame.size.height/2, customBadge1.frame.size.width, customBadge1.frame.size.height)];