2013-10-31 15 views
5

Próbuję dodać customBadge jako podrzędny o UIButton -Dodawanie podrzędny do UIButton

To jest mój kod do tej pory -

//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]; 

Przycisk Próbuję dodać subview to _MsgHeadBtn, który jest ikoną e-maila na górze LH na zrzucie ekranu poniżej. Próbowałem sprawić, by niestandardowy znaczek pojawił się nieco wyżej i na prawo od ikony e-maila - ale w efekcie otrzymam zrzut ekranu!

enter image description here

Czy ktoś może zaoferować jakąś radę na to, gdzie idę źle !?

Odpowiedz

5

Problem należy do metody setFrame:. Używasz self.view.frame.size.width.

Sprawdź z tym kodem:

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

lub

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

powiedziałbym: '[customBadge1 setFrame: CGRectMake (_MsgHeadBtn.frame .size.width, 0, customBadge1.frame.size.width, customBadge1.frame.size.height)]; ' – sergio

+0

@sergio: Dzięki za informację. Właśnie zmodyfikowałem swoją odpowiedź :) –

+0

działa - miło! – Dancer

1

Regulacja ramki jak poniżej:

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