Question

I'm trying to add a badge to the navigation bar of my iOS app,more precisely to the Menu button which triggers my side menu. I'm using this code

// add badge to the menu button
if (!self.numberBadge) {
         self.numberBadge = [[MLPAccessoryBadgeEmboss alloc] initWithFrame:CGRectZero];
       }
          numberBadge.center = CGPointMake(30.0, 6);
          numberBadge.badgeMinimumSize = CGSizeMake(1.0, 1.0);
          numberBadge.backgroundColor = [UIColor redColor];
          numberBadge.shadowAlpha = 0.9;
          numberBadge.cornerRadius = 5.0f;
          numberBadge.strokeColor = [UIColor whiteColor];
          [self.navigationController.navigationBar addSubview:numberBadge];
          [numberBadge setTextWithIntegerValue:9]; 

as result, the badge gets displayed like that:

the badge

now I've been trying both modifying the frame and the centre of the badge uiview but, no matter what I do, it will stick to the upper-left corner of the navigation bar. It's currently impossible for me to move it.

Any suggestions?

Was it helpful?

Solution

It looks like your badge is always positioned at (0, 0) in the navigation bar.

I assume that when you assign self.numberBadge, you are assigning it to an instance variable called numberBadge, otherwise you would had to use self.numberBadge instead.

I would try these things:

  • creating the view with a frame not CGRectZero, but the frame where you want to place the badge. Sometimes views created with frame zero haven't behaved as I expected.
  • try moving the badge center after the call to addSubview, just to make sure it won't mess with your frame.

If it still doesn't work, can you try adding the badge to a plain UIView and check if it works there?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top