Add a CustomBadge to a UISegmentedControl in the navigationBar

杀马特。学长 韩版系。学妹 提交于 2019-12-23 03:27:07

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/11702159/add-a-custombadge-to-a-uisegmentedcontrol-in-the-navigationbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!