5

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!?

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Dancer
  • 17,035
  • 38
  • 129
  • 206

2 Answers2

5

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];
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • I would say: ` [customBadge1 setFrame:CGRectMake(_MsgHeadBtn.frame.size.width, 0, customBadge1.frame.size.width, customBadge1.frame.size.height)];` – sergio Oct 31 '13 at 11:11
  • @sergio: Thanks for the info. I just modified my answer :) – Midhun MP Oct 31 '13 at 11:17
  • @Dancer: With pleasure, when you add subView, it's frame will be calculated based on it's parent's frame. – Midhun MP Oct 31 '13 at 11:32
  • @Dancer: if clicked, you can get the instance in the IBAction method using - (IBAction)click:(UIButton *)sender{//sender is the clicked button} – Midhun MP Oct 31 '13 at 12:15
1

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)];
manujmv
  • 6,450
  • 1
  • 22
  • 35