1

There is an UISegmentedControl in a navigationBar, which created in the Storyboard and have an Outlet wired to it. I have tried to add a custom badge to the UISegmentedControl, but failed. The badge does not appear.

Ps. The custom badge appears when i add it to the navigationBar (the superview of the UISegmentedControl), but it is the second approach for me. I wanna add it directly to the UISegmentedControl, could I?

MyTableViewController.h

...

@interface MyTableViewController : UITableViewController{
} 

@property (strong,nonatomic) IBOutlet UISegmentedControl  *segmentedControl;

...

MyTableViewController.m

@synthesize segmentedControl;

...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CustomBadge *customBadge = [CustomBadge customBadgeWithString:@"1" withStringColor:[UIColor whiteColor] withInsetColor:[UIColor orangeColor] withBadgeFrame:YES withBadgeFrameColor:[UIColor whiteColor] withScale:0.8 withShining:YES];

NSLog(@"self.segmentedControl :%@",self.segmentedControl);
NSLog(@"self.segmentedControl w: %f, h :%f",self.segmentedControle.frame.size.width, self.segmentedControlle.frame.size.height);
NSLog(@"customBadge x: %f, y: %f, w: %f, h :%f", customBadge.frame.origin.x, customBadge.frame.origin.y,customBadge.frame.size.width, customBadge.frame.size.height);

    [self.segmentedControl addSubview:customBadge];
}

...

log results:

self.segmentedControl :<UISegmentedControl: 0x3b7bf0; frame = (83 7; 154 30); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x3b7c70>>
self.segmentedControl w: 154.000000, h :30.000000
customBadge x: 0.000000, y: 0.000000, w: 20.000000, h :20.000000

the CustomBadge is the third party custom badge view class.

lu yuan
  • 7,207
  • 9
  • 44
  • 78
  • Someone else had a similar problem recently - please test that self.segmentedControl is not nil. Also, log the frame of the segmented control as well as the frame of the badge and add it to your question. – David H Jul 28 '12 at 15:23
  • @DavidH Plz check the edit in my post:) – lu yuan Jul 28 '12 at 15:46
  • hi @luyuan - just reread your PS. let's simplify and add a simple UIView to your segmentedControl. .backgroundColor = [UIColor redColor]; something that will show up. – danh Jul 28 '12 at 15:55
  • @danh Yes, I have tried to add some simple imageView, but doesn't appear. Problem solved as you seen in the David's answer:) – lu yuan Jul 28 '12 at 17:15

2 Answers2

3

Very good! Thanks. Now, it COULD be that a segmented control is not a true view in the sense that its content is reflected in subviews (there are such things on the mac, like a tab view) - they manage an array of views, and thus they don't really pay any attention to subviews when they draw in the drawRect method. So this control may be drawing over your badge in its drawRect. You will have to probe further.

Even so, there is a solution, which is to create a container UIView of the same size, add the segmented control first, then add your custom badge second, then add that container view to the UINavigationBar. That should work.

David H
  • 40,852
  • 12
  • 92
  • 138
  • I think you've got it right, @David. That's where I was going with trying to add a simple UIVew, just to see if segmented control had a normal view hierarchy. And I agree with the workaround. +1 – danh Jul 28 '12 at 16:21
  • Also, hope you're not offended by this (and I realize the forum isn't thrilled with this kind of comment), but I think it's _really_ cool that somebody age 61 is writing iOS code. I've noticed that many engineers don't keep their skills up-to-date much past half that age. I hope to emulate you. – danh Jul 28 '12 at 16:24
  • Hah - thanks for looking. Almost 62 now. I love my work - I commute 2 hours each way to NYC to work too! That's why I have so much time to answer questions :-) I use to do embedded systems, but work dried up in USA, I switched to Mac work (hard to find but interesting), then moved to iOS when the jobs started showing up. Keep current! I still buy mid level books and read them (Matt Neubergs book of late), go to WWDC, and read Apples docs constantly. I'm older than most of my co-workers parents! – David H Jul 28 '12 at 16:29
  • Yeah, I'm a real Apple zealot :-) – David H Jul 28 '12 at 17:22
0

I've never used CutomBadge, but I think you need to set the frame.

danh
  • 62,181
  • 10
  • 95
  • 136