Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a subview to UIButton

I'm trying to add a customBadge as a subview of a UIButton -

this is my code so far -

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

The button I'm trying to add the subview to is _MsgHeadBtn, which is the email icon on top LH of the screenshot below. I was trying to make the custom badge appear slighty above and to the right of the email icon - but I end up with the result in the screenshot!

enter image description here

Can anyone offer any advice as to where i'm going wrong!?

like image 581
Dancer Avatar asked Oct 31 '13 11:10

Dancer


2 Answers

Issue is within your setFrame: method. You are using self.view.frame.size.width.

Check with this code:

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

or

[customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width, 0, customBadge1.frame.size.width, customBadge1.frame.size.height)];
[_MsgHeadBtn addSubview:customBadge1];
like image 162
Midhun MP Avatar answered Sep 27 '22 01:09

Midhun MP


Adjust your frame like below:

[customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width-customBadge1.frame.size.width,-customBadge1.frame.size.height/2, customBadge1.frame.size.width, customBadge1.frame.size.height)];
like image 39
manujmv Avatar answered Sep 24 '22 01:09

manujmv