简体   繁体   中英

Add a CustomBadge to a UISegmentedControl in the navigationBar

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.

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.

我从未使用过CutomBadge,但我认为你需要设置框架。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM